Java HashMap get() Method

The HashMap.get(Object key) method in Java is used to retrieve the value associated with a specific key in a HashMap. This guide will cover the method's usage, explain how it works, and provide examples to demonstrate its functionality.

Table of Contents

  1. Introduction
  2. get Method Syntax
  3. Examples
    • Retrieving Values from a HashMap
    • Real-World Use Case: Employee Detail Lookup
  4. Conclusion

Introduction

The get(Object key) method is a member of the HashMap class in Java. It allows you to retrieve the value associated with a specific key in the HashMap. If the key is not found, the method returns null.

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 type Object, 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 in a HashMap.

Example

import java.util.HashMap;

public class GetExample {
    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 from the HashMap
        Integer ageRavi = people.get("Ravi");
        Integer ageAmit = people.get("Amit"); // 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 "null"
    }
}

Output:

Age of Ravi: 25
Age of Amit: null

Real-World Use Case: Employee Detail Lookup

In a real-world scenario, you might use the get method to look up employee details based on their employee ID.

Example

import java.util.HashMap;

public class EmployeeDetailLookup {
    public static void main(String[] args) {
        // Creating a HashMap with String keys (employee IDs) and String values (employee names)
        HashMap<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 employee details from the database
        String employeeName = employeeDatabase.get(employeeIdToSearch);

        // Checking if the employee ID exists
        if (employeeName != null) {
            System.out.println("Employee ID " + employeeIdToSearch + " corresponds to: " + employeeName);
        } else {
            System.out.println("Employee ID " + employeeIdToSearch + " does not exist in the database.");
        }
    }
}

Output:

Employee ID E002 corresponds to: Priya Sharma

Conclusion

The HashMap.get(Object key) method in Java provides a way to retrieve the value associated with a specific key in a HashMap. By understanding how to use this method, you can efficiently access values stored in a HashMap based on their keys. This method is useful in various scenarios, such as looking up employee details or retrieving specific data points based on unique identifiers.

Comments