Understanding Map Interface in Java
The Map interface in Java is a part of the Java Collection Framework (JCF) but is not a part of the Collection interface. A Map is used to store key-value pairs, where each key is unique, and each key maps to at most one value.
In this blog, we will explore the Map interface, its key methods, and its implementations: HashMap, LinkedHashMap, TreeMap, and Hashtable.
What is the Map Interface?#
The Map interface is present in the java.util package and is used to store data in the form of key-value pairs.
Key Features of Map#
- Stores Key-Value Pairs: Each key is unique and maps to a single value.
- Efficient Lookup: Searching for a value by key is fast.
- No Duplicate Keys: A key can map to only one value at a time.
- Allows Null Keys and Values (depending on the implementation).
Key Methods of Map Interface#
| Method | Description |
|---|---|
put(K key, V value) | Inserts a key-value pair into the map. |
get(Object key) | Retrieves the value associated with a key. |
remove(Object key) | Removes the key-value pair from the map. |
containsKey(Object key) | Checks if a key is present in the map. |
containsValue(Object value) | Checks if a value is present in the map. |
size() | Returns the number of key-value pairs in the map. |
clear() | Removes all elements from the map. |
Implementations of Map Interface
1. HashMap (Unordered & Fast)#
Characteristics:#
- Uses a HashTable internally.
- No guarantee of insertion order.
- Allows one null key and multiple null values.
- Fast access (O(1) time complexity for
put,get,remove).
Example:#
Output:
Note: Order may vary since HashMap does not maintain insertion order.
2. LinkedHashMap (Ordered & Fast)#
Characteristics:#
- Extends
HashMapbut maintains insertion order. - Uses a doubly linked list alongside a HashTable.
- Faster than TreeMap but slightly slower than HashMap.
Example:#
Output:
Note: The order of elements remains the same as insertion order.
3. TreeMap (Sorted Order)#
Characteristics:#
- Implements
SortedMapinterface. - Maintains elements in sorted (ascending) order.
- Uses a Red-Black Tree (Self-balancing BST).
- Slower than HashMap but allows sorted retrieval.
Example:#
Output:
Note: Elements are sorted in ascending order of keys.
4. Hashtable (Thread-Safe but Slow)#
Characteristics:#
- Synchronized and thread-safe.
- Does not allow null keys or values.
- Slower than HashMap due to synchronization overhead.
Example:#
Output:
Note: Unlike HashMap, it does not allow null keys or values.
Choosing the Right Map Implementation#
| Feature | HashMap | LinkedHashMap | TreeMap | Hashtable |
|---|---|---|---|---|
| Order Maintained? | No | Yes (Insertion Order) | Yes (Sorted Order) | No |
| Speed (Put/Get/Delete) | Fastest | Fast | Slowest (Balanced Tree) | Slow (Thread-safe) |
Allows null? | Yes (1 key, multiple values) | Yes (1 key, multiple values) | No | No |
| Thread-Safe? | No | No | No | Yes |
| Internal Structure | HashTable | HashTable + Linked List | Red-Black Tree | Synchronized HashTable |
Conclusion#
In this blog, we explored the Map interface and its implementations: HashMap, LinkedHashMap, TreeMap, and Hashtable.
- Use
HashMapwhen fast lookup is required, and ordering is not important. - Use
LinkedHashMapwhen maintaining insertion order is needed. - Use
TreeMapwhen sorting is necessary but at the cost of performance. - Use
Hashtablewhen thread-safety is required but at the cost of speed.
In the upcoming blogs we will go deep into each of is implementation classes.