entrySet() , values() and keySet()
import java.util.Iterator;
import java.util.Map.Entry;
import java.util.TreeMap;
public class mapTest{
public static void main(String [] args){
TreeMap<String , Integer> map = new TreeMap<String ,Integer>();
map.put("a", 1);
map.put("b", 2);
map.put("c", 3);
map.put("d", 4);
// values()
Iterator<Integer> iter1 = map.values().iterator();
while(iter1.hasNext()){
System.out.print(iter1.next() + " ");
}
System.out.println();
//entrySet()
Iterator<Entry<String,Integer> >iter2 = map.entrySet().iterator();
Entry<String,Integer > one;
while(iter2.hasNext()){
one = iter2.next();
System.out.print(one.getKey()+":"+one.getValue()+ " ");
}
System.out.println();
// keySet()
Iterator<String> iter3 = map.keySet().iterator();
while(iter3.hasNext()){
System.out.print(iter3.next()+ " ");
}
System.out.println();
}
}
Difference between List and Set
- List can contain two items that have common value , while Set can't .
- Sort Method :
-- List : Collections.sort(list , ...);
-- Set : new TreeSet ( new Comparator() {...} )