Java HashMap entrySet() Method

The HashMap.entrySet() method in Java is used to obtain a set view of the mappings contained in the HashMap.

Table of Contents

  1. Introduction
  2. entrySet Method Syntax
  3. Examples
    • Iterating Over Entries in a HashMap
    • Real-World Use Case: Displaying Employee Records
  4. Conclusion

Introduction

The HashMap.entrySet() method is a member of the HashMap class in Java. It allows you to obtain a set view of the key-value pairs contained in the HashMap. This can be useful for iterating over the entries in the map or performing bulk operations on the entries.

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 view of the key-value pairs contained in the HashMap.

Examples

Iterating Over Entries in a HashMap

The entrySet method can be used to obtain a set view of the entries in a HashMap, which can then be iterated over.

Example

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

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

        // Getting the set view of the entries
        Set<Map.Entry<String, String>> entries = countries.entrySet();

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

Output:

Key: India, Value: IN
Key: United States, Value: US
Key: Canada, Value: CA

Real-World Use Case: Displaying Employee Records

In a real-world scenario, you might use the entrySet method to display employee records stored in an employee database.

Example

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

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

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

        // Iterating over the entries to display employee records
        for (Map.Entry<String, String> entry : entries) {
            System.out.println("Employee ID: " + entry.getKey() + ", Employee Name: " + entry.getValue());
        }
    }
}

Output:

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 obtain a set view of the key-value pairs contained in the HashMap. By understanding how to use this method, you can efficiently iterate over the entries and perform bulk operations on the entries in the map. This method is useful for displaying records, performing operations on each entry, and managing collections of key-value pairs.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top