Java HashMap keySet() Method

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

Table of Contents

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

Introduction

The HashMap.keySet() method is a member of the HashMap class in Java. It allows you to obtain a set view of the keys contained in the HashMap. This can be useful for iterating over the keys or performing bulk operations on the keys.

keySet() Method Syntax

The syntax for the keySet method is as follows:

public Set<K> keySet()
  • The method does not take any parameters.
  • The method returns a Set view of the keys contained in the HashMap.

Examples

Iterating Over Keys in a HashMap

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

Example

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

public class KeySetExample {
    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 keys
        Set<String> keys = countries.keySet();

        // Iterating over the keys
        for (String key : keys) {
            System.out.println("Key: " + key);
        }
    }
}

Output:

Key: India
Key: United States
Key: Canada

Real-World Use Case: Displaying Employee IDs

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

Example

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

public class DisplayEmployeeIDs {
    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 keys
        Set<String> employeeIDs = employeeDatabase.keySet();

        // Iterating over the keys to display employee IDs
        for (String employeeID : employeeIDs) {
            System.out.println("Employee ID: " + employeeID);
        }
    }
}

Output:

Employee ID: E001
Employee ID: E002
Employee ID: E003

Conclusion

The HashMap.keySet() method in Java provides a way to obtain a set view of the keys contained in the HashMap. By understanding how to use this method, you can efficiently iterate over the keys and perform bulk operations on the keys in the map. This method is useful for displaying keys, performing operations on each key, and managing collections of keys in various applications.

Leave a Comment

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

Scroll to Top