Java – TreeMap (Collections Framework)

TreeMap: A map-based collection that stores key-value pairs in sorted order.
Example:

import java.util.TreeMap;
TreeMap map = new TreeMap<>();
map.put(3, "Alice");
map.put(1, "John");
map.put(2, "Bob");
System.out.println(map);
map.remove(3);
System.out.println(map);
for (int key : map.keySet()) {
System.out.println(key + " -> " + map.get(key));
}

No images available.