Java EnumMap keySet() Method

The EnumMap.keySet() method in Java is used to return a set view of the keys contained in the map. This guide will cover the method's usage with examples, and we will also cover a real-world use case to show how EnumMap.keySet() can be used effectively.

Table of Contents

  1. Introduction
  2. keySet Method Syntax
  3. Examples
    • Basic Usage of keySet Method
    • Iterating Over Keys
  4. Real-World Use Case
    • Example: Listing Available Roles in a Company
  5. Conclusion

Introduction

The EnumMap.keySet() method is a member of the EnumMap class in Java. It returns a set view of the keys contained in the map, allowing you to access all keys stored in the EnumMap.

keySet() Method Syntax

The syntax for the keySet method is as follows:

public Set<K> keySet()
  • Returns: A set view of the keys contained in this map.

Examples

Basic Usage of keySet Method

The keySet method can be used to retrieve a set of all keys contained in an EnumMap.

Example

import java.util.EnumMap;
import java.util.Set;

public class EnumMapKeySetExample {
    // Define an enum representing months of the year
    enum Month {
        JANUARY, FEBRUARY, MARCH, APRIL, MAY, JUNE, JULY, AUGUST, SEPTEMBER, OCTOBER, NOVEMBER, DECEMBER
    }

    public static void main(String[] args) {
        // Create an EnumMap with Month as key and String as value
        EnumMap<Month, String> holidays = new EnumMap<>(Month.class);

        // Adding entries to the EnumMap
        holidays.put(Month.JANUARY, "New Year's Day");
        holidays.put(Month.DECEMBER, "Christmas Day");

        // Retrieving the set of keys
        Set<Month> keys = holidays.keySet();

        // Printing the set of keys
        System.out.println("Keys in holidays map: " + keys);
    }
}

Output:

Keys in holidays map: [JANUARY, DECEMBER]

Iterating Over Keys

You can use the keySet method to iterate over the keys in an EnumMap.

Example

import java.util.EnumMap;
import java.util.Set;

public class EnumMapIterateKeysExample {
    // Define an enum representing types of transportation
    enum Transportation {
        CAR, BUS, TRAIN, AIRPLANE, BICYCLE
    }

    public static void main(String[] args) {
        // Create an EnumMap with Transportation as key and Integer as value
        EnumMap<Transportation, Integer> transportMap = new EnumMap<>(Transportation.class);

        // Adding entries to the EnumMap
        transportMap.put(Transportation.CAR, 50);
        transportMap.put(Transportation.BUS, 30);
        transportMap.put(Transportation.TRAIN, 80);

        // Retrieving the set of keys
        Set<Transportation> keys = transportMap.keySet();

        // Iterating over the keys and printing them
        for (Transportation key : keys) {
            System.out.println("Key: " + key + ", Value: " + transportMap.get(key));
        }
    }
}

Output:

Key: CAR, Value: 50
Key: BUS, Value: 30
Key: TRAIN, Value: 80

Real-World Use Case

Example: Listing Available Roles in a Company

A common real-world use case for EnumMap.keySet() is listing all the available roles in a company and their respective counts.

Example

import java.util.EnumMap;
import java.util.Set;

public class CompanyRoles {
    // Define an enum representing employee roles
    enum Role {
        ENGINEER, MANAGER, HR, SALES, MARKETING
    }

    public static void main(String[] args) {
        // Create an EnumMap to manage counts of each employee role
        EnumMap<Role, Integer> employeeRoles = new EnumMap<>(Role.class);

        // Adding roles to the EnumMap
        employeeRoles.put(Role.ENGINEER, 5);
        employeeRoles.put(Role.MANAGER, 2);
        employeeRoles.put(Role.HR, 3);
        employeeRoles.put(Role.SALES, 4);
        employeeRoles.put(Role.MARKETING, 1);

        // Retrieving the set of keys
        Set<Role> roles = employeeRoles.keySet();

        // Listing all roles in the company
        System.out.println("Available roles in the company:");
        for (Role role : roles) {
            System.out.println(role + ": " + employeeRoles.get(role) + " positions");
        }
    }
}

Output:

Available roles in the company:
ENGINEER: 5 positions
MANAGER: 2 positions
HR: 3 positions
SALES: 4 positions
MARKETING: 1 position

In this example, EnumMap.keySet() is used to list all available roles in a company and their respective counts, making it easy to see the distribution of roles.

Conclusion

The EnumMap.keySet() method in Java provides a way to get a set view of the keys contained in the map. By understanding how to use this method, you can efficiently access and iterate over the keys in collections where the keys are enum constants. This method allows you to manage and utilize the keys in an EnumMap, making it a versatile tool for managing data in various scenarios.

Comments