Java HashMap entrySet() Method

The HashMap.entrySet() method in Java is used to get a Set view of the mappings contained in the 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. entrySet Method Syntax
  3. Examples
    • Iterating Over Entries in a HashMap
    • Real-World Use Case: Displaying Employee Details
  4. Conclusion

Introduction

The HashMap.entrySet() method is a member of the HashMap class in Java. It returns a Set view of the mappings contained in the HashMap. Each element in the returned set is a Map.Entry object, which represents a key-value pair in the HashMap.

entrySet() Method Syntax

The syntax for the entrySet method is as follows:

public Set<Map.Entry<K, V>> entrySet()
  • The method does not take any parameters.
  • The method returns a Set of Map.Entry<K, V> objects, representing the key-value pairs in the HashMap.

Examples

Iterating Over Entries in a HashMap

The entrySet method can be used to iterate over the entries in a HashMap.

Example

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

public class EntrySetExample {
    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);

        // Getting the entry set
        Set<Map.Entry<String, Integer>> entrySet = people.entrySet();

        // Iterating over the entry set
        for (Map.Entry<String, Integer> entry : entrySet) {
            System.out.println("Key: " + entry.getKey() + ", Value: " + entry.getValue());
        }
    }
}

Output:

Key: Ravi, Value: 25
Key: Priya, Value: 30
Key: Vijay, Value: 35

Real-World Use Case: Displaying Employee Details

In a real-world scenario, you might use the entrySet method to display details of employees stored in a HashMap.

Example

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

public class DisplayEmployeeDetails {
    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");

        // Getting the entry set
        Set<Map.Entry<String, String>> entrySet = employeeDatabase.entrySet();

        // Displaying employee details
        System.out.println("Employee Details:");
        for (Map.Entry<String, String> entry : entrySet) {
            System.out.println("Employee ID: " + entry.getKey() + ", Employee Name: " + entry.getValue());
        }
    }
}

Output:

Employee Details:
Employee ID: E001, Employee Name: Ravi Kumar
Employee ID: E002, Employee Name: Priya Sharma
Employee ID: E003, Employee Name: Vijay Singh

Conclusion

The HashMap.entrySet() method in Java provides a way to get a Set view of the mappings contained in the HashMap. By understanding how to use this method, you can efficiently iterate over the entries in a HashMap and perform operations on each key-value pair. This method is useful in various scenarios, such as displaying data, processing entries, or converting the map's contents into another format.

Comments