In java Set, List and Map are three important interface of Java collection
framework and also there are many interview questions around these.
In this post i will provide a simple difference between Set,List and Map.
List :
It is an ordered collections and allows duplicate values.
Set
It is an unordered collections and does not allows duplicate values.
Map
Store the datas in the form of values and keys. Each value will have the unique key.
I have provided a simple example how list,set and map works in java.
import java.util.*;
public class HelloWorld{
public static void main(String []args){
//Implementation of lists
List l = new ArrayList();
l.add ("trivi");
l.add ("trivi");
Iterator it = l.iterator();
while (it.hasNext()){
Object o = it.next();
System.out.println ("Iterating the list items :"+o);
}
System.out.println ();
//Implementation of set
Set s = new HashSet();
s.add("triviset");
s.add("triviset");
s.add("triviset1");
Iterator its = s.iterator();
while (its.hasNext()){
Object os = its.next();
System.out.println ("Iterating the set items :"+os);
}
System.out.println ();
//Implementation of Map
Map m1= new HashMap();
m1.put("1","trivim");
m1.put("3","trivim");
m1.put("3","trivim1");
m1.put("4","trivim");
System.out.println ("Map items are :"+m1);
Set ms = m1.keySet();
Iterator itm = ms.iterator();
while (itm.hasNext()){
Object om = itm.next();
System.out.println ("Iterating the map :"+om);
}
}
}
Output obtained is :
Note :
Map cannot be iterated though it can iterated through the keyset.
Hope this might be useful. For any querries revert back to asadrina@gmail.com
In this post i will provide a simple difference between Set,List and Map.
List :
It is an ordered collections and allows duplicate values.
Set
It is an unordered collections and does not allows duplicate values.
Map
Store the datas in the form of values and keys. Each value will have the unique key.
I have provided a simple example how list,set and map works in java.
import java.util.*;
public class HelloWorld{
public static void main(String []args){
//Implementation of lists
List l = new ArrayList();
l.add ("trivi");
l.add ("trivi");
Iterator it = l.iterator();
while (it.hasNext()){
Object o = it.next();
System.out.println ("Iterating the list items :"+o);
}
System.out.println ();
//Implementation of set
Set s = new HashSet();
s.add("triviset");
s.add("triviset");
s.add("triviset1");
Iterator its = s.iterator();
while (its.hasNext()){
Object os = its.next();
System.out.println ("Iterating the set items :"+os);
}
System.out.println ();
//Implementation of Map
Map m1= new HashMap();
m1.put("1","trivim");
m1.put("3","trivim");
m1.put("3","trivim1");
m1.put("4","trivim");
System.out.println ("Map items are :"+m1);
Set ms = m1.keySet();
Iterator itm = ms.iterator();
while (itm.hasNext()){
Object om = itm.next();
System.out.println ("Iterating the map :"+om);
}
}
}
Output obtained is :
Iterating the list items :trivi
Iterating the list items :trivi
Iterating the set items :triviset
Iterating the set items :triviset1
Map items are :{3=trivim1, 1=trivim, 4=trivim}
Iterating the map :3
Iterating the map :1
Iterating the map :4
Note :
Map cannot be iterated though it can iterated through the keyset.
Hope this might be useful. For any querries revert back to asadrina@gmail.com