tags: Collection, Data Structures, Hashing

What is the Map interface in Java?

Map is a part of the Java Collections Framework that stores key-value pairs.
Each key is unique, and each key maps to exactly one value.

What are the key features of Map?

  • Stores data as (key, value) pairs
  • Keys must be unique, but values can be duplicated
  • Allows one null key and multiple null values (depending on implementation)

Which classes implement the Map interface?

  • HashMap
  • LinkedHashMap
  • TreeMap
  • Hashtable
  • ConcurrentHashMap
  • WeakHashMap
  • EnumMap

What is the difference between Map and Collection?

  • Map is not a subtype of Collection
  • Collection stores elements; Map stores key-value pairs
  • You can convert a Map to a Set or Collection via keySet(), values(), or entrySet()

What are the main methods of the Map interface?

MethodDescription
put(K key, V value)Adds or replaces a key-value pair
get(Object key)Returns value for the key
remove(Object key)Removes entry for the key
containsKey(Object key)Checks if key exists
containsValue(Object v)Checks if value exists
keySet()Returns a Set of all keys
values()Returns a Collection of all values
entrySet()Returns Set of key-value entries

What is HashMap in Java?

HashMap is a commonly used implementation of Map backed by a hash table.

  • It allows one null key and multiple null values.
  • It does not maintain order.

What is LinkedHashMap?

A subclass of HashMap that maintains insertion order of entries.
Useful when you want predictable iteration order.

What is TreeMap?

A Map implementation that keeps keys in sorted (natural or custom) order.
It is based on a Red-Black tree and does not allow null keys.

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

FeatureHashMapLinkedHashMapTreeMap
OrderNo orderInsertion orderSorted order
Null key allowedYes (1)Yes (1)No
PerformanceFastSlightly slowerSlower (log n)

Is Map thread-safe?

  • HashMap, LinkedHashMap, and TreeMap are not thread-safe

  • Use ConcurrentHashMap for concurrent access

  • Collections.synchronizedMap(new HashMap<>()) adds synchronization


What is ConcurrentHashMap?

A thread-safe version of HashMap that allows concurrent reads and updates without locking the entire map.
Ideal for multi-threaded applications.


What is Hashtable?

An older synchronized version of Map.
It’s thread-safe but less efficient than ConcurrentHashMap.


Can Map store null keys or values?

ImplementationNull KeyNull Value
HashMapYes (1)Yes (many)
LinkedHashMapYes (1)Yes
TreeMapNoYes
ConcurrentHashMapNoNo
HashtableNoNo

How do you iterate over a Map?

There are 3 common ways:

// Using entrySet
for (Map.Entry<K, V> entry : map.entrySet()) {
    System.out.println(entry.getKey() + " = " + entry.getValue());
}
 
// Using keySet
for (K key : map.keySet()) {
    System.out.println(key + " = " + map.get(key));
}
 
// Using forEach (Java 8+)
map.forEach((k, v) -> System.out.println(k + " = " + v));

How does HashMap handle collisions?

It uses a bucket-based structure where collisions are handled by:

  • Linked list chaining (before Java 8)

  • Tree (red-black tree) + Linked list (after threshold in Java 8)


What happens when you insert a duplicate key into a Map?

The new value replaces the old one for the same key.

map.put("A", 1);
map.put("A", 2); // now "A" maps to 2

How to sort a Map by keys?

Map<String, Integer> sortedMap = new TreeMap<>(map);

How to sort a Map by values?

map.entrySet()
   .stream()
   .sorted(Map.Entry.comparingByValue())
   .forEach(System.out::println);

How to check if a Map is empty or get its size?

map.isEmpty();   // returns true if map is empty
map.size();      // returns number of entries

What is the purpose of entrySet(), keySet(), and values()?

MethodDescription
entrySet()Set of key-value pairs (Map.Entry)
keySet()Set of keys
values()Collection of all values

What is WeakHashMap?

A Map that stores keys as weak references.
When a key is no longer referenced outside the map, it is automatically garbage collected.


What is EnumMap?

A high-performance Map specifically for enum keys.
It is compact and very fast but does not allow null keys.


Can you remove entries while iterating a Map?

Yes, using the Iterator from entrySet():

Iterator<Map.Entry<K, V>> it = map.entrySet().iterator();
while (it.hasNext()) {
    Map.Entry<K, V> entry = it.next();
    if (entry.getValue() < 0) {
        it.remove();
    }
}

What are some real-world use cases of Map?

  • Caching data (e.g., user sessions, config settings)

  • Grouping and counting items

  • Lookup tables

  • Storing key-value structured data like JSON


Ready to move to the next topic — Iterator & Iterable?