Java EnumSet copyOf Method

The EnumSet.copyOf method in Java is used to create a copy of an existing collection or another EnumSet. This guide will cover the method's usage with examples, and we will also cover the overloaded versions of the EnumSet.copyOf method to show the various ways you can create a copy of an EnumSet.

Table of Contents

  1. Introduction
  2. copyOf Method Syntax
  3. Examples
    • Basic Usage of copyOf Method with Collection
    • Basic Usage of copyOf Method with EnumSet
  4. Real-World Use Case
    • Example: Managing Duplicated Days of the Week Sets
  5. Conclusion

Introduction

The EnumSet.copyOf method is a static factory method of the EnumSet class in Java. It allows you to create an EnumSet that is a copy of an existing collection or another EnumSet. This can be useful when you need to duplicate an existing set of enum constants.

copyOf() Method Syntax

The syntax for the copyOf method is as follows:

Copying from a Collection

public static <E extends Enum<E>> EnumSet<E> copyOf(Collection<E> c)

Copying from another EnumSet

public static <E extends Enum<E>> EnumSet<E> copyOf(EnumSet<E> s)

Examples

Basic Usage of copyOf Method with Collection

The copyOf method can be used to create an EnumSet that is a copy of an existing collection.

Example

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

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

    public static void main(String[] args) {
        // Creating a list of days
        List<Day> daysList = new ArrayList<>();
        daysList.add(Day.MONDAY);
        daysList.add(Day.WEDNESDAY);
        daysList.add(Day.FRIDAY);

        // Creating an EnumSet by copying the list
        EnumSet<Day> daysEnumSet = EnumSet.copyOf(daysList);

        // Printing the EnumSet
        System.out.println("EnumSet copied from list: " + daysEnumSet);
    }
}

Output:

EnumSet copied from list: [MONDAY, WEDNESDAY, FRIDAY]

Basic Usage of copyOf Method with EnumSet

The copyOf method can be used to create an EnumSet that is a copy of another EnumSet.

Example

import java.util.EnumSet;

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

    public static void main(String[] args) {
        // Creating an original EnumSet
        EnumSet<Fruit> originalSet = EnumSet.of(Fruit.APPLE, Fruit.BANANA, Fruit.ORANGE);

        // Creating a copy of the original EnumSet
        EnumSet<Fruit> copiedSet = EnumSet.copyOf(originalSet);

        // Printing the copied EnumSet
        System.out.println("Copied EnumSet: " + copiedSet);
    }
}

Output:

Copied EnumSet: [APPLE, BANANA, ORANGE]

Real-World Use Case

Example: Managing Duplicated Days of the Week Sets

A common real-world use case for EnumSet.copyOf() is managing duplicated sets of days of the week for different purposes, such as scheduling or task management.

Example

import java.util.EnumSet;

public class DuplicatedDaysManager {
    // 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 original EnumSet of selected days
        EnumSet<Day> selectedDays = EnumSet.of(Day.MONDAY, Day.WEDNESDAY, Day.FRIDAY);

        // Creating a copy of the selected days EnumSet
        EnumSet<Day> duplicatedDays = EnumSet.copyOf(selectedDays);

        // Printing the original and duplicated EnumSets
        System.out.println("Original selected days: " + selectedDays);
        System.out.println("Duplicated selected days: " + duplicatedDays);

        // Checking if a specific day is in the duplicated set
        boolean isWednesdayDuplicated = duplicatedDays.contains(Day.WEDNESDAY);
        System.out.println("Is Wednesday in the duplicated set? " + isWednesdayDuplicated);
    }
}

Output:

Original selected days: [MONDAY, WEDNESDAY, FRIDAY]
Duplicated selected days: [MONDAY, WEDNESDAY, FRIDAY]
Is Wednesday in the duplicated set? true

In this example, EnumSet.copyOf() is used to create a duplicate set of selected days, making it easy to manage multiple sets with the same values.

Conclusion

The EnumSet.copyOf method in Java provides a way to create an EnumSet that is a copy of an existing collection or another EnumSet. By understanding how to use this method and its overloaded versions, you can efficiently create and manage duplicates of collections of enum constants. 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