Swiftorial Logo
Home
Swift Lessons
Tutorials
Learn More
Career
Resources

Java FAQ: Top Questions

42. What is the difference between HashMap, LinkedHashMap, and TreeMap?

HashMap , LinkedHashMap , and TreeMap are implementations of the Map interface in java.util , differing in ordering, performance, and use cases.

  • HashMap:
    • Unordered key-value pairs.
    • Fast for lookups, inserts, and deletes (O(1) average).
    • Allows one null key and multiple null values.
  • LinkedHashMap:
    • Maintains insertion order (or access order with configuration).
    • Slightly slower than HashMap due to linked list maintenance.
    • Useful for LRU caches.
  • TreeMap:
    • Sorted by keys (natural order or custom Comparator ).
    • Slower operations (O(log n)) due to red-black tree.
    • No null keys; null values allowed.