
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Duplicate Key Handling in HashMap Object in Java
No, HashMap does not allow duplicate keys. The HashMap is a class that implements the Map interface. It is based on the Hash table. It is used to store key-value pairs. It allows null keys and values, but it does not allow duplicate keys.
You can store key-value pairs in the HashMap object. Once you do so, you can retrieve the values of the respective keys, but the values we use for keys should be unique. Let's understand what will happen if we try to add a duplicate key into a HashMap.
Addng a duplicate key to a HashMap
The put() method is used to add key-value pairs to the current HashMap object. Following is the syntax -
put(key, value)
This method associates the value with the specified key. i.e.,
- If we add a key-value pair where the key is not present, the key-value pair is inserted.
- But if the key exists already, this method replaces the existing value of the key with the new value.
Example
Let us look at a Java program where we add duplicate values to a HashMap. Here we have inserted the names and mobile numbers of 5 people, displayed the same, and then we are inserting two more key-value pairs where the keys are duplicates of the previously inserted ones.
So, here the new value overwrites or replaces the existing values for the same keys.
import java.util.HashMap; import java.util.Iterator; import java.util.Map; public class DuplicatesInHashMap { public static void main(String args[]) { HashMap < String, Long > map = new HashMap < String, Long > (); map.put("Krishna", 9000123456L); map.put("Rama", 9000234567L); map.put("Sita", 9000345678L); map.put("Bhima", 9000456789L); map.put("Yousuf", 9000456789L); // Removed extra space in "Yousuf" System.out.println("Values Stored . . . . . ."); // Retrieving the values of the HashMap Iterator < Map.Entry < String, Long >> it1 = map.entrySet().iterator(); System.out.println("Contents of the HashMap are: "); while (it1.hasNext()) { Map.Entry < String, Long > ele = it1.next(); System.out.print(ele.getKey() + " : "); System.out.println(ele.getValue()); } // Adding duplicate keys with new values map.put("Bhima", 0000000000L); map.put("Rama", 0000000000L); // Retrieving the values of the HashMap after modifications Iterator < Map.Entry < String, Long >> it2 = map.entrySet().iterator(); System.out.println("Contents of the HashMap after inserting new key-value pair: "); while (it2.hasNext()) { Map.Entry < String, Long > ele = it2.next(); System.out.print(ele.getKey() + " : "); System.out.println(ele.getValue()); } } }
On compiling, the above program gives you the following output.
Values Stored . . . . . . Contents of the hashMap are: Yousuf : 9000456789 Krishna : 9000123456 Sita : 9000345678 Rama : 9000234567 Bhima : 9000456789 Contents of the hashMap after inserting new key-value pair: Yousuf : 9000456789 Krishna : 9000123456 Sita : 9000345678 Rama : 0 Bhima : 0
Preventing Overwrites with putIfAbsent() Method
The HashMap class contains another method named putIfAbsent(). This is similar to the put() method; it also accepts key-value pairs as parameters and adds them to the current HashMap object. The only difference is that this method inserts values only if the given key does not already exist in the HashMap.
Example
In the following program, we are trying to insert a key-value pair into a HashMap only if the key is not already present.
import java.util.HashMap; import java.util.Iterator; import java.util.Map; public class InsertIfNotPresent { public static void main(String[] args) { // Creating a HashMap HashMap < String, String > map = new HashMap < > (); // Inserting key-value pairs map.put("Rama", "Engineer"); map.put("Bhima", "Doctor"); // Inserting key-value pair only if the key is not already present map.putIfAbsent("Rama", "Teacher"); // This won't change the value for "Rama" map.putIfAbsent("Charlie", "Lawyer"); // This will add the key-value pair for "Charlie" // Using Iterator to display the content of the map System.out.println("Contents of the HashMap:"); Iterator < Map.Entry < String, String >> it = map.entrySet().iterator(); while (it.hasNext()) { Map.Entry < String, String > entry = it.next(); System.out.println(entry.getKey() + ": " + entry.getValue()); } } }
On compiling, the above program gives you the following output.
Contents of the HashMap: Rama: Engineer Bhima: Doctor Charlie: Lawyer