HashMap.getOrDefault(Object key, V defaultValue)
method in Java is used to return the value to which the specified key is mapped, or defaultValue
if the map contains no mapping for the key. This guide will cover the method's usage, explain how it works, and provide examples to demonstrate its functionality.Table of Contents
- Introduction
getOrDefault
Method Syntax- Examples
- Retrieving Values with Default in a HashMap
- Real-World Use Case: Default Product Price
- Conclusion
Introduction
The getOrDefault()
method is a member of the HashMap
class in Java. It provides a way to retrieve the value associated with a specified key, returning a default value if the key is not found. This is useful for avoiding null
values and providing a fallback when the key is not present in the map.
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 key is not found.
- The method returns the value associated with the specified key, or
defaultValue
if the map contains no mapping for the key.
Examples
Retrieving Values with Default in a HashMap
The getOrDefault
method can be used to retrieve the value associated with a key, with a fallback to a default value if the key is not found.
Example
import java.util.HashMap;
public class GetOrDefaultExample {
public static void main(String[] args) {
// Creating a HashMap with String keys and Integer values
HashMap<String, Integer> people = new HashMap<>();
// Adding entries to the HashMap
people.put("Ravi", 25);
people.put("Priya", 30);
people.put("Vijay", 35);
// Retrieving values with default
int ageRavi = people.getOrDefault("Ravi", 0);
int ageAmit = people.getOrDefault("Amit", 0); // Key "Amit" does not exist
// Printing the results
System.out.println("Age of Ravi: " + ageRavi);
System.out.println("Age of Amit: " + ageAmit); // This will print the default value 0
}
}
Output:
Age of Ravi: 25
Age of Amit: 0
Real-World Use Case: Default Product Price
In a real-world scenario, you might use the getOrDefault
method to retrieve the price of a product, returning a default price if the product ID is not found in the database.
Example
import java.util.HashMap;
public class DefaultProductPrice {
public static void main(String[] args) {
// Creating a HashMap with String keys (product IDs) and Integer values (prices)
HashMap<String, Integer> productPrices = new HashMap<>();
// Adding entries to the HashMap
productPrices.put("P001", 1000);
productPrices.put("P002", 1500);
productPrices.put("P003", 2000);
// Default price to be used if the product ID is not found
int defaultPrice = 500;
// Retrieving product prices with default
int priceP001 = productPrices.getOrDefault("P001", defaultPrice);
int priceP004 = productPrices.getOrDefault("P004", defaultPrice); // Key "P004" does not exist
// Printing the results
System.out.println("Price of P001: " + priceP001);
System.out.println("Price of P004: " + priceP004); // This will print the default price
}
}
Output:
Price of P001: 1000
Price of P004: 500
Conclusion
The getOrDefault()
method in Java provides a way to retrieve the value associated with a specified key, returning a default value if the key is not found. By understanding how to use this method, you can efficiently manage missing keys and provide fallback values, avoiding potential null
values in your application. This method is useful in various scenarios, such as providing default settings, handling missing data, or setting default values for computations.
Comments
Post a Comment
Leave Comment