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.
Map Interface Examples (Quick Reference)
1. Creating a Map using HashMap
import java.util.*;
class Demo {
public static void main(String[] args) {
Map<Integer, String> map = new HashMap<>();
map.put(1, "Java");
map.put(2, "API");
System.out.println(map);
}
}
Output:
{1=Java, 2=API}
2. Map Does NOT Allow Duplicate Keys
import java.util.*;
class Demo {
public static void main(String[] args) {
Map<Integer, String> map = new HashMap<>();
map.put(1, "Java");
map.put(1, "Selenium"); // overwrite
System.out.println(map);
}
}
Output:
{1=Selenium}
3. Map Allows Duplicate Values
import java.util.*;
class Demo {
public static void main(String[] args) {
Map<Integer, String> map = new HashMap<>();
map.put(1, "Test");
map.put(2, "Test");
System.out.println(map);
}
}
4. HashMap Allows One null Key
import java.util.*;
class Demo {
public static void main(String[] args) {
Map<String, String> map = new HashMap<>();
map.put(null, "Value");
map.put("A", null);
System.out.println(map);
}
}
Output:
{null=Value, A=null}
5. Accessing Value Using get()
import java.util.*;
class Demo {
public static void main(String[] args) {
Map<Integer, String> map = new HashMap<>();
map.put(1, "Java");
System.out.println(map.get(1));
}
}
Output:
Java
6. Checking Key & Value Existence
import java.util.*;
class Demo {
public static void main(String[] args) {
Map<Integer, String> map = Map.of(1, "A", 2, "B");
System.out.println(map.containsKey(1));
System.out.println(map.containsValue("B"));
}
}
Output:
true
true
7. Iterating Map Using entrySet() (Most Common)
import java.util.*;
class Demo {
public static void main(String[] args) {
Map<Integer, String> map = Map.of(1, "Java", 2, "API");
for (Map.Entry<Integer, String> e : map.entrySet()) {
System.out.println(e.getKey() + " -> " + e.getValue());
}
}
}
8. Iterating Using keySet()
import java.util.*;
class Demo {
public static void main(String[] args) {
Map<Integer, String> map = Map.of(1, "A", 2, "B");
for (Integer key : map.keySet()) {
System.out.println(key + " = " + map.get(key));
}
}
}
9. Iterating Using values()
import java.util.*;
class Demo {
public static void main(String[] args) {
Map<Integer, String> map = Map.of(1, "X", 2, "Y");
for (String value : map.values()) {
System.out.println(value);
}
}
}
10. LinkedHashMap – Maintains Insertion Order
import java.util.*;
class Demo {
public static void main(String[] args) {
Map<Integer, String> map = new LinkedHashMap<>();
map.put(2, "B");
map.put(1, "A");
System.out.println(map);
}
}
Output:
{2=B, 1=A}
11. TreeMap – Sorted by Keys
import java.util.*;
class Demo {
public static void main(String[] args) {
Map<Integer, String> map = new TreeMap<>();
map.put(3, "C");
map.put(1, "A");
map.put(2, "B");
System.out.println(map);
}
}
Output:
{1=A, 2=B, 3=C}
12. TreeMap Does NOT Allow null Key
import java.util.*;
class Demo {
public static void main(String[] args) {
Map<Integer, String> map = new TreeMap<>();
// map.put(null, "X"); // ❌ NullPointerException
}
}
13. putIfAbsent()
import java.util.*;
class Demo {
public static void main(String[] args) {
Map<Integer, String> map = new HashMap<>();
map.put(1, "Java");
map.putIfAbsent(1, "API");
map.putIfAbsent(2, "API");
System.out.println(map);
}
}
Output:
{1=Java, 2=API}
14. replace() Method
import java.util.*;
class Demo {
public static void main(String[] args) {
Map<Integer, String> map = new HashMap<>();
map.put(1, "Java");
map.replace(1, "Selenium");
System.out.println(map);
}
}
Output:
{1=Selenium}
15. remove(key, value) (Conditional Remove)
import java.util.*;
class Demo {
public static void main(String[] args) {
Map<Integer, String> map = new HashMap<>();
map.put(1, "Java");
map.remove(1, "API"); // no effect
map.remove(1, "Java"); // removed
System.out.println(map);
}
}
Output:
{}
16. computeIfAbsent()
import java.util.*;
class Demo {
public static void main(String[] args) {
Map<Integer, String> map = new HashMap<>();
map.computeIfAbsent(1, k -> "Generated");
System.out.println(map);
}
}
17. Sorting Map by Key Using TreeMap
import java.util.*;
class Demo {
public static void main(String[] args) {
Map<Integer, String> map = new HashMap<>();
map.put(2, "B");
map.put(1, "A");
Map<Integer, String> sorted = new TreeMap<>(map);
System.out.println(sorted);
}
}
18. Custom Object as Key (equals & hashCode)
import java.util.*;
class User {
int id;
User(int id) { this.id = id; }
public boolean equals(Object o) {
return this.id == ((User) o).id;
}
public int hashCode() {
return id;
}
}
class Demo {
public static void main(String[] args) {
Map<User, String> map = new HashMap<>();
map.put(new User(1), "Admin");
map.put(new User(1), "Admin");
System.out.println(map.size());
}
}
Output:
1
19. Thread-Safe Map
import java.util.*;
class Demo {
public static void main(String[] args) {
Map<String, String> map =
Collections.synchronizedMap(new HashMap<>());
map.put("A", "Safe");
System.out.println(map);
}
}
20. Interview Summary – Map Interface
import java.util.*;
class Demo {
public static void main(String[] args) {
Map<Integer, String> map = new HashMap<>();
map.put(1, "Key");
map.put(2, "Value");
System.out.println(map);
}
}
Key Points
- Stores key–value pairs
- Keys are unique
- Values can be duplicated
- HashMap → fast, unordered
- LinkedHashMap → insertion order
- TreeMap → sorted keys
Key Takeaway
Use Map when fast key-based lookup is required. Choosing the correct implementation directly affects performance, ordering, and scalability.