The LinkedHashMap.getOrDefault(Object key, V defaultValue)
method in Java is used to retrieve the value associated with a specific key in a LinkedHashMap
, or return a default value if the key is not present.
Table of Contents
- Introduction
getOrDefault
Method Syntax- Examples
- Retrieving Existing Values
- Handling Non-Existent Keys
- Real-World Use Case
- Example: Fetching Default User Preferences
- Conclusion
Introduction
The LinkedHashMap.getOrDefault(Object key, V defaultValue)
method is a member of the LinkedHashMap
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 in scenarios where you need to ensure a value is always returned, even if the key is not found 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 Existing Values
The getOrDefault
method can be used to retrieve existing values from a LinkedHashMap
.
Example
import java.util.LinkedHashMap;
public class GetOrDefaultExample {
public static void main(String[] args) {
// Creating a LinkedHashMap with String keys and Integer values
LinkedHashMap<String, Integer> people = new LinkedHashMap<>();
// Adding entries to the LinkedHashMap
people.put("Ravi", 25);
people.put("Priya", 30);
people.put("Vijay", 35);
// Retrieving existing values using getOrDefault
Integer ageRavi = people.getOrDefault("Ravi", 0);
Integer agePriya = people.getOrDefault("Priya", 0);
// Printing the retrieved values
System.out.println("Age of Ravi: " + ageRavi);
System.out.println("Age of Priya: " + agePriya);
}
}
Output:
Age of Ravi: 25
Age of Priya: 30
Handling Non-Existent Keys
The getOrDefault
method returns the specified default value if the key is not found in the LinkedHashMap
.
Example
import java.util.LinkedHashMap;
public class NonExistentKeyExample {
public static void main(String[] args) {
// Creating a LinkedHashMap with String keys and Integer values
LinkedHashMap<String, Integer> people = new LinkedHashMap<>();
// Adding entries to the LinkedHashMap
people.put("Ravi", 25);
people.put("Priya", 30);
people.put("Vijay", 35);
// Attempting to retrieve a value for a non-existent key using getOrDefault
Integer ageAmit = people.getOrDefault("Amit", 0);
// Printing the result
System.out.println("Age of Amit: " + ageAmit);
}
}
Output:
Age of Amit: 0
Real-World Use Case
Example: Fetching Default User Preferences
A common real-world use case for LinkedHashMap.getOrDefault(Object key, V defaultValue)
is fetching default user preferences when a specific preference is not set by the user. For example, let's consider a scenario where user preferences are stored in a LinkedHashMap
, and we need to fetch preferences with a default value if a preference is not set.
Example
import java.util.LinkedHashMap;
public class UserPreferences {
public static void main(String[] args) {
// Creating a LinkedHashMap to store user preferences
LinkedHashMap<String, String> preferences = new LinkedHashMap<>();
// Adding some preferences to the LinkedHashMap
preferences.put("theme", "dark");
preferences.put("notifications", "enabled");
// Fetching preferences with default values
String themePreference = preferences.getOrDefault("theme", "light");
String notificationsPreference = preferences.getOrDefault("notifications", "disabled");
String fontSizePreference = preferences.getOrDefault("fontSize", "medium");
// Printing the preferences
System.out.println("Theme: " + themePreference);
System.out.println("Notifications: " + notificationsPreference);
System.out.println("Font Size: " + fontSizePreference);
}
}
Output:
Theme: dark
Notifications: enabled
Font Size: medium
In this example, LinkedHashMap.getOrDefault(Object key, V defaultValue)
is used to fetch user preferences with default values if certain preferences are not set, demonstrating how to handle both existing and non-existent keys.
Conclusion
The LinkedHashMap.getOrDefault(Object key, V defaultValue)
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 ensure that a value is always returned, even if the key is not found, making it a versatile tool for data management in your Java applications.
Comments
Post a Comment
Leave Comment