The HashMap.getOrDefault()
method in Java is used to return the value to which the specified key is mapped, or the default value if the map contains no mapping for the key.
Table of Contents
- Introduction
getOrDefault
Method Syntax- Examples
- Retrieving Values with a Default
- Real-World Use Case: Handling Missing Employee Records
- Conclusion
Introduction
The HashMap.getOrDefault()
method is a member of the HashMap
class in Java. It allows you to retrieve the value associated with a specific key, or return a default value if the key is not present in the map. This can be useful for handling cases where a key might not be present without throwing an exception or returning null
.
getOrDefault() Method Syntax
The syntax for the getOrDefault
method is as follows:
public V getOrDefault(Object key, V defaultValue)
- The method takes two parameters:
key
of typeObject
, which represents the key whose associated value is to be returned.defaultValue
of typeV
, which represents the value to be returned if the map contains no mapping for the key.
- The method returns the value associated with the specified key, or the
defaultValue
if the map contains no mapping for the key.
Examples
Retrieving Values with a Default
The getOrDefault
method can be used to retrieve the value associated with a specific key, or return a default value if the key is not present.
Example
import java.util.HashMap;
import java.util.Map;
public class GetOrDefaultExample {
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");
countries.put("Canada", "CA");
// Retrieving values with a default value
String indiaCode = countries.getOrDefault("India", "Unknown");
String brazilCode = countries.getOrDefault("Brazil", "Unknown");
// Printing the results
System.out.println("Country code for India: " + indiaCode);
System.out.println("Country code for Brazil: " + brazilCode);
}
}
Output:
Country code for India: IN
Country code for Brazil: Unknown
Real-World Use Case: Handling Missing Employee Records
In a real-world scenario, you might use the getOrDefault
method to handle cases where an employee ID might not be present in the employee database, returning a default message instead.
Example
import java.util.HashMap;
import java.util.Map;
public class EmployeeRecordRetrieval {
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");
employeeDatabase.put("E003", "Vijay Singh");
// Employee ID to be searched
String employeeIdToSearch1 = "E002";
String employeeIdToSearch2 = "E004";
// Retrieving the employee name using the employee ID, with a default message
String employeeName1 = employeeDatabase.getOrDefault(employeeIdToSearch1, "Employee not found");
String employeeName2 = employeeDatabase.getOrDefault(employeeIdToSearch2, "Employee not found");
// Printing the employee names
System.out.println("Employee ID " + employeeIdToSearch1 + ": " + employeeName1);
System.out.println("Employee ID " + employeeIdToSearch2 + ": " + employeeName2);
}
}
Output:
Employee ID E002: Priya Sharma
Employee ID E004: Employee not found
Conclusion
The HashMap.getOrDefault()
method in Java provides a way to retrieve the value associated with a specific key, or return a default value if the key is not present in the map. By understanding how to use this method, you can handle cases where a key might not be present without throwing an exception or returning null
. This method is useful in various scenarios, such as handling missing records, providing default values, and managing collections of data.