Curriculum
HashMap and HashSet are two of the most important data structures in the Java Collections Framework. They are widely used in enterprise applications, Spring Boot projects, REST APIs, microservices, banking systems, e-commerce platforms, and large-scale backend applications because of their excellent performance and efficient data retrieval capabilities.
While HashMap stores data in key-value pairs, HashSet stores unique values without duplicates. Both use a hashing mechanism internally, which allows operations such as insertion, deletion, and lookup to be performed very efficiently.
Understanding HashMap and HashSet is essential for every Java Backend Engineer because these collections are among the most frequently used data structures in professional software development and technical interviews.
Before understanding HashMap and HashSet, it is important to understand hashing.
Hashing is a technique used to convert data into a unique numeric value called a hash code.
Example:
String name = "Rahul";
System.out.println(name.hashCode());
Output:
Various Integer Value
The hash code helps Java determine where data should be stored in memory.
Hashing enables fast searching and retrieval operations.
Hashing provides:
Modern enterprise applications depend heavily on hashing techniques.
HashMap is a collection class that stores data as key-value pairs.
Example:
HashMap<Integer, String> students =
new HashMap<>();
Data can be stored as:
101 -> Rahul
102 -> Priya
103 -> Amit
The key identifies the value.
HashMap is one of the most commonly used collections in Java.
HashMap provides:
Most backend applications use HashMap extensively.
Example:
HashMap<Integer, String> students =
new HashMap<>();
Using the Map interface:
Map<Integer, String> students =
new HashMap<>();
This approach follows object-oriented design principles.
Example:
students.put(101, "Rahul");
students.put(102, "Priya");
students.put(103, "Amit");
Output concept:
{
101=Rahul,
102=Priya,
103=Amit
}
The put() method inserts data into the map.
Example:
System.out.println(students.get(102));
Output:
Priya
The value is retrieved using its key.
This operation is extremely fast.
Example:
students.put(102, "Neha");
Output:
102=Neha
The old value is replaced.
Example:
students.remove(101);
The specified key-value pair is removed.
Example:
students.containsKey(102);
Output:
true
This method checks whether a key exists.
Example:
students.containsValue("Priya");
Output:
true
This verifies whether a value exists.
Example:
students.size();
Output:
3
Returns the number of entries.
Example:
for(Map.Entry<Integer, String> entry :
students.entrySet()) {
System.out.println(
entry.getKey() +
" " +
entry.getValue()
);
}
Output:
101 Rahul
102 Priya
103 Amit
Iteration is commonly used in backend applications.
Internally, HashMap uses:
Hash Table
The process:
Example:
Key → HashCode → Bucket
This mechanism provides fast access.
Example:
students.put(null, "Admin");
Valid.
HashMap allows:
This behavior is important to remember.
Example:
students.put(101, "Rahul");
students.put(101, "Amit");
Output:
101=Amit
Duplicate keys are not allowed.
The latest value replaces the previous value.
Example:
HashMap<Integer, String> products =
new HashMap<>();
Data:
products.put(101, "Laptop");
products.put(102, "Mouse");
products.put(103, "Keyboard");
Retrieve:
System.out.println(products.get(101));
Output:
Laptop
HashMap is ideal for inventory management.
HashSet is a collection class used to store unique elements.
Example:
HashSet<String> skills =
new HashSet<>();
Unlike HashMap:
No Key
Only Values
HashSet automatically removes duplicates.
HashSet provides:
These features make it useful in many applications.
Example:
HashSet<String> cities =
new HashSet<>();
Using Set interface:
Set<String> cities =
new HashSet<>();
This is the preferred approach.
Example:
cities.add("Jaipur");
cities.add("Delhi");
cities.add("Mumbai");
Output:
[Jaipur, Delhi, Mumbai]
Elements are added successfully.
Example:
cities.add("Delhi");
cities.add("Delhi");
Output:
Delhi
Only one copy is stored.
HashSet automatically removes duplicates.
Example:
cities.contains("Delhi");
Output:
true
The contains() method is extremely fast.
Example:
cities.remove("Delhi");
The specified value is removed.
Example:
cities.size();
Output:
3
Returns the total number of unique elements.
Example:
for(String city : cities) {
System.out.println(city);
}
Output:
Jaipur
Delhi
Mumbai
Iteration is simple and efficient.
HashSet internally uses:
HashMap
Every value becomes a key in an internal HashMap.
Example concept:
Delhi -> PRESENT
Mumbai -> PRESENT
Jaipur -> PRESENT
This design enables fast operations.
Example:
cities.add(null);
Valid.
HashSet allows:
One Null Value
Additional null entries are ignored.
Example:
cities.add("Delhi");
cities.add("Mumbai");
cities.add("Jaipur");
Output order may vary.
HashSet does not guarantee insertion order.
For ordered storage:
LinkedHashSet
should be used.
Example:
HashSet<String> skills =
new HashSet<>();
Data:
skills.add("Java");
skills.add("Spring");
skills.add("Java");
Output:
Java
Spring
Duplicate skills are automatically removed.
Although both use hashing, they serve different purposes.
Stores:
Key + Value
Stores:
Only Values
Duplicate keys not allowed.
Duplicate values not allowed.
Retrieve by key.
Example:
products.get(101);
No direct indexing.
Search using:
contains()
Uses:
Hash Table
Uses:
HashMap Internally
| Feature | HashMap | HashSet |
|---|---|---|
| Storage | Key-Value Pairs | Unique Values |
| Duplicate Keys | Not Allowed | N/A |
| Duplicate Values | Allowed | Not Allowed |
| Null Support | One Null Key | One Null Value |
| Ordering | Not Guaranteed | Not Guaranteed |
| Retrieval | By Key | By Value Search |
Use HashMap when:
Examples:
Product Inventory
Customer Records
Configuration Settings
User Sessions
Use HashSet when:
Examples:
Unique Skills
Unique Email IDs
Unique Usernames
Unique Product Categories
Spring Boot applications frequently use:
Map<String, Object> response;
Set<String> permissions;
These collections are essential in enterprise development.
These benefits make both collections extremely useful.
HashSet does not maintain insertion order.
Use:
LinkedHashSet
instead.
Duplicate keys overwrite existing values.
Incorrect:
HashMap map =
new HashMap();
Correct:
HashMap<Integer, String> map =
new HashMap<>();
Generics improve type safety.
These practices improve software quality and maintainability.
HashMap and HashSet are widely used in:
Map<AccountId, Account>
Map<ProductId, Product>
Set<String> Roles
Set<String> Permissions
Map<Integer, Product>
Modern backend applications rely heavily on these collections.
HashMap and HashSet are powerful collection classes that use hashing to provide efficient data storage and retrieval. HashMap stores data as key-value pairs, while HashSet stores unique values and automatically prevents duplicates.
Understanding hashing, internal implementation, performance characteristics, and real-world use cases helps developers choose the right collection for their applications. Mastering HashMap and HashSet is essential for Java backend development, enterprise software engineering, and technical interviews.
HashMap stores key-value pairs, while HashSet stores only unique values.
No. Duplicate keys overwrite existing values.
No. HashSet automatically removes duplicate values.
Yes. HashMap allows one null key and multiple null values.
They provide fast data storage, retrieval, duplicate prevention, and efficient application performance.
Want to explore additional programming and software development topics? Click here for more free courses
WhatsApp us