🎓 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.clear() method in Java is used to remove all elements from the EnumSet, making it empty. This guide will cover the method's usage with examples, and we will also cover a real-world use case to show how EnumSet.clear() can be used effectively.
Table of Contents
- Introduction
clearMethod Syntax- Examples
- Basic Usage of
clearMethod - Clearing an EnumSet with Multiple Elements
- Basic Usage of
- Real-World Use Case
- Example: Resetting Selected Days of the Week
- Conclusion
Introduction
The EnumSet.clear() method is a member of the EnumSet class in Java. It allows you to remove all elements from the EnumSet, leaving it empty. This can be useful in scenarios where you need to reset or reinitialize the set.
clear() Method Syntax
The syntax for the clear method is as follows:
public void clear()
- Parameters: This method does not take any parameters.
- Returns: This method does not return a value.
Examples
Basic Usage of clear Method
The clear method can be used to remove all elements from an EnumSet.
Example
import java.util.EnumSet;
public class EnumSetClearExample {
// 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);
// Printing the EnumSet before clearing
System.out.println("EnumSet before clearing: " + days);
// Clearing the EnumSet
days.clear();
// Printing the EnumSet after clearing
System.out.println("EnumSet after clearing: " + days);
}
}
Output:
EnumSet before clearing: [MONDAY, WEDNESDAY, FRIDAY]
EnumSet after clearing: []
Clearing an EnumSet with Multiple Elements
You can use the clear method to remove all elements from an EnumSet that contains multiple elements.
Example
import java.util.EnumSet;
public class EnumSetClearMultipleExample {
// 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);
// Printing the EnumSet before clearing
System.out.println("EnumSet before clearing: " + fruits);
// Clearing the EnumSet
fruits.clear();
// Printing the EnumSet after clearing
System.out.println("EnumSet after clearing: " + fruits);
}
}
Output:
EnumSet before clearing: [APPLE, BANANA, ORANGE, MANGO]
EnumSet after clearing: []
Real-World Use Case
Example: Resetting Selected Days of the Week
A common real-world use case for EnumSet.clear() is resetting a set of selected days of the week in a scheduling application.
Example
import java.util.EnumSet;
public class SelectedDaysReset {
// 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);
// Printing the selected days before resetting
System.out.println("Selected days before resetting: " + selectedDays);
// Clearing the selected days
selectedDays.clear();
// Printing the selected days after resetting
System.out.println("Selected days after resetting: " + selectedDays);
}
}
Output:
Selected days before resetting: [MONDAY, WEDNESDAY, FRIDAY, SUNDAY]
Selected days after resetting: []
In this example, EnumSet.clear() is used to reset the set of selected days, making it easy to start with a fresh set of days.
Conclusion
The EnumSet.clear() method in Java provides a way to remove all elements from the EnumSet, leaving it empty. By understanding how to use this method, you can efficiently manage collections of enum constants and handle scenarios where you need to reset or reinitialize the set. 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