Java EnumSet complementOf() Method

The EnumSet.complementOf() method in Java is used to create an EnumSet containing all elements of the specified enum type that are not in the given set. This guide will cover the method's usage with examples, and we will also cover a real-world use case to show how EnumSet.complementOf() can be used effectively.

Table of Contents

  1. Introduction
  2. complementOf Method Syntax
  3. Examples
    • Basic Usage of complementOf Method
    • Using Complement with Various EnumSets
  4. Real-World Use Case
    • Example: Managing Unselected Days of the Week
  5. Conclusion

Introduction

The EnumSet.complementOf(EnumSet<E> s) method is a static factory method of the EnumSet class in Java. It allows you to create an EnumSet containing all elements of the specified enum type that are not present in the given EnumSet.

complementOf() Method Syntax

The syntax for the complementOf method is as follows:

public static <E extends Enum<E>> EnumSet<E> complementOf(EnumSet<E> s)
  • Parameters:
    • s: The EnumSet whose complement is to be created.
  • Returns: An EnumSet containing all elements of the specified enum type that are not in the given set.

Examples

Basic Usage of complementOf Method

The complementOf method can be used to create an EnumSet containing all elements of an enum type that are not present in the given set.

Example

import java.util.EnumSet;

public class EnumSetComplementOfExample {
    // 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> weekdays = EnumSet.of(Day.MONDAY, Day.TUESDAY, Day.WEDNESDAY, Day.THURSDAY, Day.FRIDAY);

        // Creating a complement EnumSet
        EnumSet<Day> weekendDays = EnumSet.complementOf(weekdays);

        // Printing the complement EnumSet
        System.out.println("Weekend days: " + weekendDays);
    }
}

Output:

Weekend days: [SATURDAY, SUNDAY]

Using Complement with Various EnumSets

You can use the complementOf method with different sets to create complements of those sets.

Example

import java.util.EnumSet;

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

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

        // Creating a complement EnumSet
        EnumSet<Fruit> nonTropicalFruits = EnumSet.complementOf(tropicalFruits);

        // Printing the complement EnumSet
        System.out.println("Non-tropical fruits: " + nonTropicalFruits);
    }
}

Output:

Non-tropical fruits: [APPLE, ORANGE, GRAPE]

Real-World Use Case

Example: Managing Unselected Days of the Week

A common real-world use case for EnumSet.complementOf() is managing a set of unselected days of the week when certain days have been selected for specific tasks.

Example

import java.util.EnumSet;

public class UnselectedDaysManager {
    // 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);

        // Creating a complement EnumSet for unselected days
        EnumSet<Day> unselectedDays = EnumSet.complementOf(selectedDays);

        // Printing the unselected days
        System.out.println("Unselected days: " + unselectedDays);

        // Checking if a specific day is in the unselected days set
        boolean isTuesdayUnselected = unselectedDays.contains(Day.TUESDAY);
        System.out.println("Is Tuesday unselected? " + isTuesdayUnselected);
    }
}

Output:

Unselected days: [TUESDAY, THURSDAY, SATURDAY, SUNDAY]
Is Tuesday unselected? true

In this example, EnumSet.complementOf() is used to create a set containing all days of the week that are not in the selected days set, making it easy to manage and check unselected days.

Conclusion

The EnumSet.complementOf(EnumSet<E> s) method in Java provides a way to create an EnumSet containing all elements of a specified enum type that are not present in a given set. By understanding how to use this method, you can efficiently manage collections of enum constants and their complements. 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