Java EnumSet retainAll() Method

The EnumSet.retainAll() method in Java is used to retain only the elements in the EnumSet that are contained in the specified collection. This guide will cover the method's usage with examples, and we will also cover a real-world use case to show how EnumSet.retainAll() can be used effectively.

Table of Contents

  1. Introduction
  2. retainAll Method Syntax
  3. Examples
    • Basic Usage of retainAll Method
    • Retaining Multiple Elements in the EnumSet
  4. Real-World Use Case
    • Example: Managing Task Retention for Selected Days
  5. Conclusion

Introduction

The EnumSet.retainAll() method is a member of the EnumSet class in Java. It allows you to retain only the elements in the EnumSet that are contained in the specified collection. If the collection is modified while the operation is in progress, the behavior is undefined.

retainAll() Method Syntax

The syntax for the retainAll method is as follows:

public boolean retainAll(Collection<?> c)
  • Parameters:
    • c: The collection containing elements to be retained in this set.
  • Returns: true if this set changed as a result of the call.

Examples

Basic Usage of retainAll Method

The retainAll method can be used to retain only the elements in the EnumSet that are contained in the specified collection.

Example

import java.util.EnumSet;
import java.util.ArrayList;
import java.util.List;

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

    public static void main(String[] args) {
        // Creating an EnumSet with specific elements
        EnumSet<Day> days = EnumSet.of(Day.MONDAY, Day.WEDNESDAY, Day.FRIDAY, Day.SUNDAY);

        // Creating a list of days to retain
        List<Day> daysToRetain = new ArrayList<>();
        daysToRetain.add(Day.MONDAY);
        daysToRetain.add(Day.WEDNESDAY);

        // Retaining only the elements in the list in the EnumSet
        boolean changed = days.retainAll(daysToRetain);

        // Printing the result and the EnumSet
        System.out.println("EnumSet changed: " + changed);
        System.out.println("EnumSet after retaining: " + days);
    }
}

Output:

EnumSet changed: true
EnumSet after retaining: [MONDAY, WEDNESDAY]

Retaining Multiple Elements in the EnumSet

You can use the retainAll method to retain multiple elements in the EnumSet using various collections.

Example

import java.util.EnumSet;
import java.util.HashSet;
import java.util.Set;

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

    public static void main(String[] args) {
        // Creating an EnumSet with specific elements
        EnumSet<Fruit> fruits = EnumSet.of(Fruit.APPLE, Fruit.BANANA, Fruit.ORANGE, Fruit.MANGO);

        // Creating a set of fruits to retain
        Set<Fruit> fruitsToRetain = new HashSet<>();
        fruitsToRetain.add(Fruit.APPLE);
        fruitsToRetain.add(Fruit.MANGO);

        // Retaining only the elements in the set in the EnumSet
        boolean changed = fruits.retainAll(fruitsToRetain);

        // Printing the result and the EnumSet
        System.out.println("EnumSet changed: " + changed);
        System.out.println("EnumSet after retaining: " + fruits);
    }
}

Output:

EnumSet changed: true
EnumSet after retaining: [APPLE, MANGO]

Real-World Use Case

Example: Managing Task Retention for Selected Days

A common real-world use case for EnumSet.retainAll() is managing task retention for selected days of the week in a scheduling application.

Example

import java.util.EnumSet;
import java.util.List;
import java.util.Arrays;

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

    public static void main(String[] args) {
        // Creating an EnumSet with selected days
        EnumSet<Day> selectedDays = EnumSet.of(Day.MONDAY, Day.WEDNESDAY, Day.FRIDAY, Day.SUNDAY);

        // Creating a list of days to retain tasks for
        List<Day> daysToRetain = Arrays.asList(Day.MONDAY, Day.SUNDAY);

        // Retaining tasks for the specified days
        boolean changed = selectedDays.retainAll(daysToRetain);

        // Printing the results and the EnumSet
        System.out.println("Tasks retained for specified days: " + changed);
        System.out.println("Selected days after retaining: " + selectedDays);
    }
}

Output:

Tasks retained for specified days: true
Selected days after retaining: [MONDAY, SUNDAY]

In this example, EnumSet.retainAll() is used to manage task retention for selected days, making it easy to update and verify the selected days.

Conclusion

The EnumSet.retainAll() method in Java provides a way to retain only the elements in the EnumSet that are contained in the specified collection. By understanding how to use this method, you can efficiently manage collections of enum constants and handle batch retention. This method allows you to utilize the power of EnumSet for various scenarios, making it a versatile tool for managing collections of enum constants.

Comments