Java HashMap keySet() Method

The HashMap.keySet() method in Java is used to get a Set view of the keys 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. keySet Method Syntax
  3. Examples
    • Iterating Over Keys in a HashMap
    • Real-World Use Case: Listing Product IDs
  4. Conclusion

Introduction

The keySet() method is a member of the HashMap class in Java. It returns a Set view of the keys contained in the HashMap. This can be useful when you need to perform operations on the keys, such as iteration, validation, or collection transformation.

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 of keys (K) contained in the HashMap.

Examples

Iterating Over Keys in a HashMap

The keySet method can be used to iterate over the keys in a HashMap.

Example

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

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

        // Iterating over the key set
        System.out.println("Keys in the HashMap:");
        for (String key : keys) {
            System.out.println(key);
        }
    }
}

Output:

Keys in the HashMap:
Ravi
Priya
Vijay

Real-World Use Case: Listing Product IDs

In a real-world scenario, you might use the keySet method to list all product IDs stored in a HashMap.

Example

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

public class ProductIDListing {
    public static void main(String[] args) {
        // Creating a HashMap with String keys (product IDs) and String values (product names)
        HashMap<String, String> productDatabase = new HashMap<>();

        // Adding entries to the HashMap
        productDatabase.put("P001", "Laptop");
        productDatabase.put("P002", "Smartphone");
        productDatabase.put("P003", "Tablet");

        // Getting the key set
        Set<String> productIds = productDatabase.keySet();

        // Listing all product IDs
        System.out.println("Product IDs:");
        for (String productId : productIds) {
            System.out.println(productId);
        }
    }
}

Output:

Product IDs:
P001
P002
P003

Conclusion

The HashMap.keySet() method in Java provides a way to get a Set view of the keys contained in the HashMap. By understanding how to use this method, you can efficiently perform operations on the keys, such as iteration, validation, or collection transformation. This method is useful in various scenarios, such as listing unique identifiers, processing key sets, or converting keys into another format.

Comments