Answer
HashMap is a hash-table Map; TreeMap is a sorted NavigableMap based on a red-black tree. • HashMap has no ordering guarantee. • TreeMap orders keys by natural ordering or a Comparator. • HashMap gives average constant-time get and put; TreeMap gives logarithmic operations.
Example
Code
var map = new java.util.TreeMap<Integer, String>(); map.put(2, "two"); map.put(1, "one"); System.out.println(map.keySet());
Output
[1, 2]
Quick Revision
HashMap uses hashing without order; TreeMap keeps sorted keys.