Map Interface in Java

The Map interface represents a collection of key-value pairs. Unlike List or Set, a Map is not a true collection but an essential part of the Java Collections Framework.

Each key is unique, and each key maps to at most one value.


Characteristics


Interface Hierarchy


Map\<K, V>
|
+-- HashMap\<K, V>
+-- LinkedHashMap\<K, V>
+-- TreeMap\<K, V>
+-- EnumMap\<K extends Enum<K>, V>
+-- WeakHashMap\<K, V>
+-- IdentityHashMap\<K, V>
+-- ConcurrentHashMap\<K, V>

Common Implementations

HashMap

Map<String, String> capitals = new HashMap<>();
capitals.put("India", "New Delhi");
capitals.put("USA", "Washington");
System.out.println(capitals.get("India")); // New Delhi

LinkedHashMap

Map<Integer, String> map = new LinkedHashMap<>();
map.put(1, "One");
map.put(2, "Two");
System.out.println(map); // {1=One, 2=Two}

TreeMap

Map<Integer, String> sorted = new TreeMap<>();
sorted.put(20, "B");
sorted.put(10, "A");
System.out.println(sorted); // {10=A, 20=B}

EnumMap

enum Status { SUCCESS, ERROR }
Map<Status, String> messages = new EnumMap<>(Status.class);
messages.put(Status.SUCCESS, "Operation completed");

ConcurrentHashMap

Map<String, Integer> scores = new ConcurrentHashMap<>();
scores.put("Alice", 100);
scores.put("Bob", 90);

Core Methods

Method Description
put(K, V) Inserts a key-value pair
get(Object key) Returns value mapped to key
remove(Object key) Removes the entry with the specified key
containsKey(Object) Checks if a key exists
containsValue(Object) Checks if a value exists
keySet() Returns a Set of all keys
values() Returns a Collection of all values
entrySet() Returns a Set of key-value pairs
size() Returns number of key-value pairs
clear() Removes all entries

Convenient Map Methods

Map.of(…)

Creates a small, immutable map.

Map<String, String> countryCodes = Map.of(
    "IN", "India",
    "US", "United States"
);

Collections.emptyMap()

Returns an unmodifiable, empty map.

Map<String, String> empty = Collections.emptyMap();

Collections.singletonMap(…)

Returns a map with a single entry.

Map<String, String> single = Collections.singletonMap("ID", "12345");

Map.copyOf(…)

Creates an immutable copy of another map.

Map<String, String> copied = Map.copyOf(countryCodes);

Performance Comparison

Implementation Ordering Null Keys Thread-Safe Lookup Time
HashMap No order Yes No O(1)
LinkedHashMap Insertion order Yes No O(1)
TreeMap Sorted by key No No O(log n)
EnumMap Enum ordinal No No O(1)
ConcurrentHashMap No order No Yes O(1)

When to Use


Example

Map<Integer, String> days = new HashMap<>();
days.put(1, "Sunday");
days.put(2, "Monday");
days.put(3, "Tuesday");

System.out.println(days.get(2));       // Monday
System.out.println(days.containsKey(3)); // true
System.out.println(days.size());       // 3

Summary

The Map interface provides a powerful way to model key-value data in Java. With various implementations offering different trade-offs in ordering, performance, and thread-safety, Map is a core part of efficient and clean Java application design.

Understanding how to use and choose the right Map implementation is essential for writing scalable and readable code.


Classes
Quiz
Videos
References
Books