Map Family in Java Collections Framework
The Map family in Java is one of the most important and widely used parts of the Java Collections Framework. In almost every enterprise application, developers need a mechanism to store data in the form of:
- key-value pairs,
- unique identifiers,
- lookup structures,
- caching systems,
- configurations,
- session management,
- API responses.
To solve these requirements efficiently, Java introduced the Map interface.
Unlike List and Set, the Map family does not store individual objects directly. Instead, Maps store data using:
Key → Value pairs
This structure is extremely useful because it allows very fast searching and retrieval based on keys.
For example:
- Employee ID → Employee Object
- Username → Password
- Product ID → Product Details
- Country Code → Country Name
These relationships are naturally represented using Maps.
The Map interface belongs to:
java.util
package.
One important thing to understand is:
Map is NOT part of Collection interface
although it belongs to the Collections Framework.
This is because Map stores:
key-value mappings
instead of individual elements.
The main implementations of the Map family are:
- HashMap,
- LinkedHashMap,
- TreeMap,
- Hashtable,
- ConcurrentHashMap.
Each implementation solves different problems related to:
- ordering,
- synchronization,
- sorting,
- concurrency,
- performance.
HashMap
HashMap is the most commonly used implementation of the Map interface. It stores data using:
Hashing
HashMap provides:
- very fast insertion,
- fast retrieval,
- fast deletion.
Average complexity for operations:
O(1)
which is near constant time.
Example:
import java.util.HashMap;
public class Demo {
public static void main(String[] args) {
HashMap<Integer, String> map =
new HashMap<>();
map.put(101, "Venky");
map.put(102, "Ravi");
map.put(103, "Java");
System.out.println(map);
}
}
Output:
{101=Venky, 102=Ravi, 103=Java}
The put() method inserts key-value pairs into the map.
The get() method retrieves values using keys.
Example:
System.out.println(map.get(101));
Output:
Venky
One of the most important properties of HashMap is:
Keys must be unique
Duplicate keys are not allowed.
Example:
map.put(101, "Old");
map.put(101, "New");
Output:
{101=New}
The old value gets replaced because keys are unique.
However:
Duplicate values are allowed
HashMap allows:
- one null key,
- multiple null values.
Example:
map.put(null, "Java");
map.put(104, null);
Internal Working of HashMap
Understanding HashMap internals is extremely important for interviews.
Internally, HashMap uses:
- array of buckets,
- hashing algorithm,
- linked lists,
- balanced trees (Java 8).
When a key is inserted:
- Java calculates hashcode(),
- hash is converted into bucket index,
- data stored in bucket.
Example:
map.put(101, "Venky");
Internally:
- hashcode generated,
- bucket identified,
- entry stored.
If multiple keys map to same bucket:
Collision occurs
Earlier Java versions handled collisions using:
Linked List
Java 8 improved performance using:
Red-Black Tree
when bucket size becomes large.
This optimization improved worst-case complexity from:
O(n)
to:
O(log n)
equals() and hashCode()
HashMap heavily depends on:
hashCode()equals()
These methods are inherited from:
Object class
hashCode() determines:
bucket location
`