import java.util.Iterator;Output:
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
public class entrySet {
public static void main(String args[]) {
TreeMap< String, Integer> treemap = new TreeMap< String, Integer> ();
treemap.put("A", 1);
treemap.put("B", 2);
treemap.put("C", 3);
treemap.put("D", 4);
treemap.put("E", 5);
treemap.put("F", 6);
//the entrySet() method returns a Set object of type Map.Entry< String, Integer>
Set< Map.Entry< String, Integer> > set = treemap.entrySet();
System.out.println("With Iterator");
//you can get an Iterator
Iterator it = set.iterator();
while(it.hasNext()){
Map.Entry me = (Map.Entry)it.next();
System.out.print(me.getKey() + ": ");
System.out.println(me.getValue());
}
System.out.println("Without Iterator");
//or you can retrieve the key and its value directly without going through Iterator
for (Map.Entry< String, Integer> me : set) {
System.out.print(me.getKey() + ": ");
System.out.println(me.getValue());
}
}
}
With IteratorReferences:
A: 1
B: 2
C: 3
D: 4
E: 5
F: 6
Without Iterator
A: 1
B: 2
C: 3
D: 4
E: 5
F: 6
Please disable your ad blocker and refresh the window to use this website.