🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
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
- Introduction
copyOfMethod Syntax- Examples
- Basic Usage of
copyOfMethod with Collection - Basic Usage of
copyOfMethod with EnumSet
- Basic Usage of
- Real-World Use Case
- Example: Managing Duplicated Days of the Week Sets
- 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.
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment