Java EnumMap values() Method

The EnumMap.values() method in Java is used to return a collection view of the values contained in the map. This guide will cover the method’s usage, explain how it works, and provide examples to demonstrate its functionality using different enum constants as keys. We will also cover a real-world use case to show how EnumMap.values() can be used effectively.

Table of Contents

  1. Introduction
  2. values Method Syntax
  3. Examples
    • Basic Usage of values Method
    • Iterating Over Values
  4. Real-World Use Case
    • Example: Summing Up Task Durations
  5. Conclusion

Introduction

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

values() Method Syntax

The syntax for the values method is as follows:

public Collection<V> values()
  • Returns: A collection view of the values contained in this map.

Examples

Basic Usage of values Method

The values method can be used to retrieve a collection of all values contained in an EnumMap.

Example

import java.util.EnumMap;
import java.util.Collection;

public class EnumMapValuesExample {
    // 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 collection of values
        Collection<String> values = holidays.values();

        // Printing the collection of values
        System.out.println("Values in holidays map: " + values);
    }
}

Output:

Values in holidays map: [New Year's Day, Christmas Day]

Iterating Over Values

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

Example

import java.util.EnumMap;
import java.util.Collection;

public class EnumMapIterateValuesExample {
    // 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 collection of values
        Collection<Integer> values = fruitMap.values();

        // Iterating over the values and printing them
        System.out.println("Values in fruitMap:");
        for (Integer value : values) {
            System.out.println(value);
        }
    }
}

Output:

Values in fruitMap:
10
20
30

Real-World Use Case

Example: Summing Up Task Durations

A common real-world use case for EnumMap.values() is summing up the durations of tasks assigned for the week.

Example

import java.util.EnumMap;
import java.util.Collection;

public class TaskDurationCalculator {
    // 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 task durations for each day
        EnumMap<Day, Integer> taskDurations = new EnumMap<>(Day.class);

        // Adding task durations for each day (in hours)
        taskDurations.put(Day.MONDAY, 2);
        taskDurations.put(Day.TUESDAY, 3);
        taskDurations.put(Day.WEDNESDAY, 4);
        taskDurations.put(Day.THURSDAY, 1);
        taskDurations.put(Day.FRIDAY, 5);

        // Retrieving the collection of task durations
        Collection<Integer> durations = taskDurations.values();

        // Summing up the task durations
        int totalDuration = 0;
        for (Integer duration : durations) {
            totalDuration += duration;
        }

        // Printing the total duration of tasks for the week
        System.out.println("Total duration of tasks for the week: " + totalDuration + " hours");
    }
}

Output:

Total duration of tasks for the week: 15 hours

In this example, EnumMap.values() is used to retrieve the task durations and sum them up to find the total duration of tasks for the week.

Conclusion

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

Leave a Comment

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

Scroll to Top