Back to Lessons

Map Interface HashMap TreeMap

April 5, 2026

Map Interface

Key-value pair collections with unique keys.

Map Examples

Map hashMap = new HashMap<>();
Map treeMap = new TreeMap<>();

hashMap.put("Java", 1);
hashMap.put("Python", 2);

Integer value = hashMap.get("Java"); // 1
hashMap.containsKey("Java"); // true

Key Points

  • HashMap: unordered, null keys allowed.
  • TreeMap: sorted by keys.
  • LinkedHashMap: insertion order.
  • Keys must have proper hashCode() equals().