The HashMap.putIfAbsent() method in Java is used to insert a key-value pair into the HashMap only if the specified key is not already associated with a value.
Table of Contents
- Introduction
putIfAbsentMethod Syntax- Examples
- Inserting Entries if Absent
- Real-World Use Case: Adding New Employee Records
- Conclusion
Introduction
The HashMap.putIfAbsent() method is a member of the HashMap class in Java. It allows you to insert a key-value pair into the map only if the key is not already present. This can be useful for avoiding overwriting existing values.
putIfAbsent() Method Syntax
The syntax for the putIfAbsent method is as follows:
public V putIfAbsent(K key, V value)
- The method takes two parameters:
keyof typeK, which represents the key to be inserted.valueof typeV, which represents the value to be associated with the key.
- The method returns the previous value associated with the specified key, or
nullif there was no mapping for the key.
Examples
Inserting Entries if Absent
The putIfAbsent method can be used to insert a key-value pair into the HashMap only if the key is not already present.
Example
import java.util.HashMap;
import java.util.Map;
public class PutIfAbsentExample {
public static void main(String[] args) {
// Creating a Map with String keys and String values
Map<String, String> countries = new HashMap<>();
// Adding entries to the HashMap
countries.put("India", "IN");
countries.put("United States", "US");
// Using putIfAbsent method
countries.putIfAbsent("Canada", "CA");
countries.putIfAbsent("India", "IND"); // This will not overwrite the existing value
// Printing the updated HashMap
System.out.println("Updated HashMap: " + countries);
}
}
Output:
Updated HashMap: {India=IN, United States=US, Canada=CA}
Real-World Use Case: Adding New Employee Records
In a real-world scenario, you might use the putIfAbsent method to add new employee records to an employee database, ensuring that existing records are not overwritten.
Example
import java.util.HashMap;
import java.util.Map;
public class AddEmployeeRecords {
public static void main(String[] args) {
// Creating a Map with String keys (employee IDs) and String values (employee names)
Map<String, String> employeeDatabase = new HashMap<>();
// Adding entries to the HashMap
employeeDatabase.put("E001", "Ravi Kumar");
employeeDatabase.put("E002", "Priya Sharma");
// Using putIfAbsent method to add new employee records
employeeDatabase.putIfAbsent("E003", "Vijay Singh");
employeeDatabase.putIfAbsent("E002", "Amit Patel"); // This will not overwrite the existing value
// Printing the updated employee database
System.out.println("Updated Employee Database: " + employeeDatabase);
}
}
Output:
Updated Employee Database: {E001=Ravi Kumar, E002=Priya Sharma, E003=Vijay Singh}
Conclusion
The HashMap.putIfAbsent() method in Java provides a way to insert key-value pairs into a HashMap only if the key is not already present. By understanding how to use this method, you can avoid overwriting existing values and ensure data integrity. This method is useful in various scenarios, such as adding new records, managing collections of data, and implementing caching mechanisms.