The LinkedHashMap.get()
method in Java is used to retrieve the value associated with a specific key in a LinkedHashMap
.
Table of Contents
- Introduction
get
Method Syntax- Examples
- Retrieving Values from a LinkedHashMap
- Handling Non-Existent Keys
- Real-World Use Case
- Example: Retrieving User Profile Information
- Conclusion
Introduction
The LinkedHashMap.get()
method is a member of the LinkedHashMap
class in Java. It allows you to retrieve the value associated with a specific key. If the key is not found, the method returns null
. This can be useful in scenarios where you need to access values based on their keys.
get() Method Syntax
The syntax for the get
method is as follows:
public V get(Object key)
- The method takes one parameter:
key
of typeObject
, which represents the key whose associated value is to be returned.
- The method returns the value associated with the specified key, or
null
if the map contains no mapping for the key.
Examples
Retrieving Values from a LinkedHashMap
The get
method can be used to retrieve values based on their keys from a LinkedHashMap
.
Example
import java.util.LinkedHashMap;
public class GetExample {
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 values from the LinkedHashMap
Integer ageRavi = people.get("Ravi");
Integer agePriya = people.get("Priya");
Integer ageVijay = people.get("Vijay");
// Printing the retrieved values
System.out.println("Age of Ravi: " + ageRavi);
System.out.println("Age of Priya: " + agePriya);
System.out.println("Age of Vijay: " + ageVijay);
}
}
Output:
Age of Ravi: 25
Age of Priya: 30
Age of Vijay: 35
Handling Non-Existent Keys
The get
method returns null
if the specified 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
Integer ageAmit = people.get("Amit");
// Checking if the key exists and printing the result
if (ageAmit == null) {
System.out.println("Key 'Amit' does not exist in the LinkedHashMap.");
} else {
System.out.println("Age of Amit: " + ageAmit);
}
}
}
Output:
Key 'Amit' does not exist in the LinkedHashMap.
Real-World Use Case
Example: Retrieving User Profile Information
A common real-world use case for LinkedHashMap.get()
is retrieving user profile information based on a username. For example, let's consider a scenario where user profiles are stored in a LinkedHashMap
, and we need to retrieve the profile information for a given username.
Example
import java.util.LinkedHashMap;
public class UserProfile {
public static void main(String[] args) {
// Creating a LinkedHashMap to store user profiles
LinkedHashMap<String, String> userProfiles = new LinkedHashMap<>();
// Adding user profiles to the LinkedHashMap
userProfiles.put("Ravi", "Profile information for Ravi.");
userProfiles.put("Priya", "Profile information for Priya.");
userProfiles.put("Vijay", "Profile information for Vijay.");
// Retrieving user profile information
String raviProfile = userProfiles.get("Ravi");
String priyaProfile = userProfiles.get("Priya");
String amitProfile = userProfiles.get("Amit"); // Non-existent key
// Printing the user profile information
System.out.println("Ravi's Profile: " + raviProfile);
System.out.println("Priya's Profile: " + priyaProfile);
if (amitProfile == null) {
System.out.println("Profile for Amit does not exist.");
} else {
System.out.println("Amit's Profile: " + amitProfile);
}
}
}
Output:
Ravi's Profile: Profile information for Ravi.
Priya's Profile: Profile information for Priya.
Profile for Amit does not exist.
In this example, LinkedHashMap.get()
is used to retrieve user profile information based on the username, demonstrating how to handle both existing and non-existent keys.
Conclusion
The LinkedHashMap.get()
method in Java provides a way to retrieve the value associated with a specific key in a LinkedHashMap
. By understanding how to use this method, you can efficiently access values based on their keys in your Java applications. The method allows you to handle both existing and non-existent keys, making it a versatile tool for data management.
Comments
Post a Comment
Leave Comment