import java.util.*;Results:
public class SortedMap {
public static void main(String[] args) {
// create a TreeMap with a generic type
TreeMaptm = new TreeMap ();
// fill treemap
tm.put(1, "one");
tm.put(2, "two");
tm.put(3, "three");
tm.put(4, "four");
tm.put(5, "five");
// retrieve all keys
Set set = tm.entrySet();
// retrieve iterator
Iterator it = set.iterator();
// browse treemap to display elements
while(it.hasNext()) {
Map.Entry mapentry = (Map.Entry)it.next();
System.out.print("["+mapentry.getKey() +", ");
System.out.println(mapentry.getValue()+"]");
}
}
}
[1, one]
[2, two]
[3, three]
[4, four]
[5, five]
Please disable your ad blocker and refresh the window to use this website.