Java EnumMap entrySet() Method

The EnumMap.entrySet() method in Java is used to return a set view of the mappings 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.entrySet() can be used effectively.

Table of Contents

  1. Introduction
  2. entrySet Method Syntax
  3. Examples
    • Basic Usage of entrySet Method
    • Iterating Over Entries
  4. Real-World Use Case
    • Example: Printing Task Assignments
  5. Conclusion

Introduction

The EnumMap.entrySet() method is a member of the EnumMap class in Java. It returns a set view of the mappings contained in the map, allowing you to access all key-value pairs stored in the EnumMap.

entrySet() Method Syntax

The syntax for the entrySet method is as follows:

public Set<Map.Entry<K,V>> entrySet()
  • Returns: A set view of the mappings contained in this map.

Examples

Basic Usage of entrySet Method

The entrySet method can be used to retrieve a set of all key-value pairs contained in an EnumMap.

Example

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

public class EnumMapEntrySetExample {
    // 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 entries
        Set<Map.Entry<Month, String>> entries = holidays.entrySet();

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

Output:

Entries in holidays map: [JANUARY=New Year's Day, DECEMBER=Christmas Day]

Iterating Over Entries

You can use the entrySet method to iterate over the key-value pairs in an EnumMap.

Example

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

public class EnumMapIterateEntriesExample {
    // Define an enum representing types of fruits
    enum Fruit {
        APPLE, BANANA, ORANGE, MANGO, GRAPE
    }

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

        // Adding entries to the EnumMap
        fruitMap.put(Fruit.APPLE, 10);
        fruitMap.put(Fruit.BANANA, 20);
        fruitMap.put(Fruit.ORANGE, 30);

        // Retrieving the set of entries
        Set<Map.Entry<Fruit, Integer>> entries = fruitMap.entrySet();

        // Iterating over the entries and printing them
        System.out.println("Entries in fruitMap:");
        for (Map.Entry<Fruit, Integer> entry : entries) {
            System.out.println("Key: " + entry.getKey() + ", Value: " + entry.getValue());
        }
    }
}

Output:

Entries in fruitMap:
Key: APPLE, Value: 10
Key: BANANA, Value: 20
Key: ORANGE, Value: 30

Real-World Use Case

Example: Printing Task Assignments

A common real-world use case for EnumMap.entrySet() is printing out all task assignments for the week.

Example

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

public class TaskPrinter {
    // Define an enum representing days of the week
    enum Day {
        MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY
    }

    public static void main(String[] args) {
        // Create an EnumMap to manage tasks for each day
        EnumMap<Day, String> tasks = new EnumMap<>(Day.class);

        // Adding tasks for each day
        tasks.put(Day.MONDAY, "Go to gym");
        tasks.put(Day.TUESDAY, "Attend meeting");
        tasks.put(Day.WEDNESDAY, "Work from home");
        tasks.put(Day.THURSDAY, "Team lunch");
        tasks.put(Day.FRIDAY, "Project presentation");
        tasks.put(Day.SATURDAY, "Family time");
        tasks.put(Day.SUNDAY, "Rest day");

        // Retrieving the set of entries
        Set<Map.Entry<Day, String>> entries = tasks.entrySet();

        // Printing all task assignments
        System.out.println("Task assignments for the week:");
        for (Map.Entry<Day, String> entry : entries) {
            System.out.println(entry.getKey() + ": " + entry.getValue());
        }
    }
}

Output:

Task assignments for the week:
MONDAY: Go to gym
TUESDAY: Attend meeting
WEDNESDAY: Work from home
THURSDAY: Team lunch
FRIDAY: Project presentation
SATURDAY: Family time
SUNDAY: Rest day

In this example, EnumMap.entrySet() is used to print out all task assignments for the week, making it easy to see the distribution of tasks.

Conclusion

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

Comments