Map Interface
java map interface HashMap LinkedHashMap TreeMap Hashtable hashCode equals load factor rehashing ConcurrentHashMap
Introduction
In the previous lesson, you learned about the Set Interface, which stores unique elements.
However, many real-world applications require storing data as key-value pairs.
For example:
| Key | Value |
|---|---|
| Employee ID | Employee Name |
| Product ID | Product Details |
| Username | Password |
| Country Code | Country Name |
| Student Roll Number | Student Record |
A List stores only values.
A Set stores only unique values.
Neither is suitable for storing associations between two pieces of information.
Java solves this problem using the Map Interface.
The Map interface stores data as key-value pairs, enabling extremely fast lookups.
Map is one of the most widely used data structures in:
- Spring Boot
- Hibernate
- REST APIs
- Banking Applications
- Caching Systems
- Authentication Systems
- Microservices
- Distributed Systems
Mastering the Map interface is essential for Java interviews.
In this lesson, you'll learn:
- What is Map?
- Why Map is Needed
- Map Hierarchy
- HashMap
- LinkedHashMap
- TreeMap
- Hashtable
- Internal Working of HashMap
- Hashing
- Buckets
- Load Factor
- Resizing (Rehashing)
- Collision Handling
- Java 8 Treeification
- equals() & hashCode()
- Performance Analysis
- Best Practices
- Common Mistakes
- Hands-on Exercises
- Professional Interview Questions
What is a Map?
A Map stores data in the form of key-value pairs.
Each key is unique.
Each key maps to exactly one value.
Example:
Map<Integer, String> employees =new HashMap<>();employees.put(101, "Rahul");employees.put(102, "Amit");employees.put(103, "Jagannath");
Retrieving:
System.out.println(employees.get(102));
Output:
Amit
Why Map?
Suppose we need to find an employee by ID.
Using List:
List<Employee> employees;
Searching requires iterating through the list.
Using Map:
Map<Integer, Employee> employees;
Searching becomes much faster.
Benefits:
- Fast lookup
- Unique keys
- Easy updates
- Efficient retrieval
- Excellent scalability
Map Hierarchy
Unlike List and Set, Map does not extend Collection.
Map│┌────────┼──────────┐│ │ │HashMap LinkedHashMap TreeMap│Hashtable
Common Map Methods
put()get()remove()containsKey()containsValue()keySet()values()entrySet()size()clear()isEmpty()
Example:
Map<Integer,String> map =new HashMap<>();map.put(1,"Java");System.out.println(map.containsKey(1));
Output:
true
HashMap
HashMap is the most commonly used implementation of the Map interface.
Characteristics:
- Fast lookups
- Unique keys
- Allows one null key
- Allows multiple null values
- No ordering guarantee
Example:
Map<String,String> capitals =new HashMap<>();capitals.put("India","New Delhi");capitals.put("Japan","Tokyo");
Internal Working of HashMap
Internally:
HashMap↓Array of Buckets↓Bucket↓Linked List↓(Red-Black Tree if heavily collided)
Each bucket stores entries based on the key's hash value.
Hashing Process
When inserting:
map.put("Java",100);
Steps:
- Compute
hashCode() - Calculate bucket index
- Check for existing key using
equals() - Replace value if key exists
- Otherwise create a new entry
Flow:
Key↓hashCode()↓Bucket↓equals()↓Store
Buckets
Example:
Bucket 0↓(Spring,100)Bucket 1↓(Java,200)Bucket 2↓(Hibernate,300)
Multiple entries may exist in the same bucket if collisions occur.
Hash Collisions
A collision occurs when two different keys map to the same bucket.
Example:
Key A↓Bucket 5Key B↓Bucket 5
Java resolves collisions using linked lists and, from Java 8 onward, converts long collision chains into balanced trees under specific conditions.
Java 8 Treeification
Before Java 8:
Bucket↓Node↓Node