← Back to Home

Map Interface

The Map interface is part of the Java Collections Framework and represents a collection of key–value pairs. Unlike List and Set, a Map does not extend the Collection interface and is designed for fast lookup using keys. This is a very high-frequency interview topic, heavily used in real-world Java applications.

What Is the Map Interface?

  • Stores data as key–value pairs
  • Keys are unique
  • Values can be duplicated
  • No index-based access
  • Part of java.util package
Map<Integer, String> map = new HashMap<>();
          

Why Map Is Needed

  • Fast data retrieval
  • Represent real-world relationships (ID → Name)
  • Efficient searching and updates
  • Widely used in caching, configuration, DB mapping

Map Hierarchy (Interview Favorite)

Map
 ├── HashMap
 ├── LinkedHashMap
 ├── TreeMap
 ├── Hashtable (legacy)
 └── ConcurrentHashMap
          

✔ Map is not a subtype of Collection

Key Characteristics of Map

  • Keys must be unique
  • Values can be duplicated
  • One null key allowed (HashMap)
  • Multiple null values allowed (HashMap)
  • Order depends on implementation

Common Implementations of Map

1️⃣ HashMap

  • Unordered
  • Fastest performance
  • Allows one null key
Map<String, Integer> map = new HashMap<>();
          

2️⃣ LinkedHashMap

  • Maintains insertion order
  • Slightly slower than HashMap
Map<String, Integer> map = new LinkedHashMap<>();
          

3️⃣ TreeMap

  • Stores keys in sorted order
  • No null keys
  • Uses Red-Black Tree
Map<String, Integer> map = new TreeMap<>();
          

4️⃣ Hashtable (Legacy)

  • Thread-safe (synchronized)
  • No null key or value
  • Rarely used now

Commonly Used Map Methods

Adding Elements

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

Accessing Elements

map.get(1);
          

Removing Elements

map.remove(2);
          

Checking Keys and Values

map.containsKey(1);
map.containsValue("Java");
          

Iterating a Map (Interview Favorite)

Using entrySet (Best Practice)

for (Map.Entry<Integer, String> entry : map.entrySet()) {
    System.out.println(entry.getKey() + " " + entry.getValue());
}
          

Using keySet

for (Integer key : map.keySet()) {
    System.out.println(key + " " + map.get(key));
}
          

Duplicate Key Handling

map.put(1, "Java");
map.put(1, "Python");

System.out.println(map); // {1=Python}
          
  • ✔ Old value replaced
  • ✔ No exception thrown

Map vs Collection (Important Difference)

Aspect Map Collection
Data format Key–Value Single element
Duplicate keys ❌ No Depends
Index-based ❌ No List only
Interface Separate Root interface

Performance Complexity (Typical)

Implementation get / put
HashMap O(1) average
LinkedHashMap O(1)
TreeMap O(log n)

Common Beginner Mistakes

  • Assuming Map preserves insertion order
  • Using mutable objects as keys
  • Forgetting to override equals() and hashCode() for keys
  • Using Hashtable unnecessarily
  • Iterating Map incorrectly

Interview-Ready Answers

Short Answer

The Map interface stores data in key–value pairs where keys are unique.

Detailed Answer

In Java, the Map interface represents a mapping between keys and values. It does not extend the Collection interface and provides implementations such as HashMap, LinkedHashMap, and TreeMap, each offering different ordering and performance characteristics.

Key Takeaway

Use Map when fast key-based lookup is required. Choosing the correct implementation directly affects performance, ordering, and scalability.