The HashMap.get()
method in Java is used to retrieve the value associated with a specific key from a HashMap
.
Table of Contents
- Introduction
get
Method Syntax- Examples
- Retrieving Values from a HashMap
- Real-World Use Case: Retrieving Employee Names
- Conclusion
Introduction
The HashMap.get()
method is a member of the HashMap
class in Java. It allows you to retrieve the value associated with a specific key. This can be useful when you need to access data stored in a HashMap
based on a key.
get() Method Syntax
The syntax for the get
method is as follows:
public V get(Object key)
- The method takes a single 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 HashMap
The get
method can be used to retrieve the value associated with a specific key from a HashMap
.
Example
import java.util.HashMap;
import java.util.Map;
public class GetExample {
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 from the HashMap
String indiaCode = countries.get("India");
String brazilCode = countries.get("Brazil"); // This will return null
// 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: null
Real-World Use Case: Retrieving Employee Names
In a real-world scenario, you might use the get
method to retrieve employee names from a company’s employee database using employee IDs.
Example
import java.util.HashMap;
import java.util.Map;
public class EmployeeNameRetrieval {
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 employeeIdToSearch = "E002";
// Retrieving the employee name using the employee ID
String employeeName = employeeDatabase.get(employeeIdToSearch);
// Printing the employee name
if (employeeName != null) {
System.out.println("Employee ID " + employeeIdToSearch + " is associated with employee name: " + employeeName);
} else {
System.out.println("Employee ID " + employeeIdToSearch + " does not exist in the database.");
}
}
}
Output:
Employee ID E002 is associated with employee name: Priya Sharma
Conclusion
The HashMap.get()
method in Java provides a way to retrieve the value associated with a specific key from a HashMap
. By understanding how to use this method, you can efficiently access data stored in a HashMap
based on keys. This method is useful in various scenarios, such as retrieving records from a database, accessing configuration settings, and managing collections of data.