🎓 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.contains() method in Java is used to check if a specified element is present in the EnumSet. This guide will cover the method's usage with examples, and we will also cover a real-world use case to show how EnumSet.contains() can be used effectively.
Table of Contents
- Introduction
containsMethod Syntax- Examples
- Basic Usage of
containsMethod - Checking for Multiple Elements
- Basic Usage of
- Real-World Use Case
- Example: Checking Selected Days of the Week
- Conclusion
Introduction
The EnumSet.contains() method is a member of the EnumSet class in Java. It allows you to check if a specified element is present in the EnumSet. The method returns true if the element is present, and false otherwise.
contains() Method Syntax
The syntax for the contains method is as follows:
public boolean contains(Object o)
- Parameters:
o: The element whose presence in this set is to be tested.
- Returns:
trueif this set contains the specified element.
Examples
Basic Usage of contains Method
The contains method can be used to check if a specified element is present in an EnumSet.
Example
import java.util.EnumSet;
public class EnumSetContainsExample {
// 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);
// Checking if specific elements are present in the EnumSet
boolean containsMonday = days.contains(Day.MONDAY);
boolean containsSunday = days.contains(Day.SUNDAY);
// Printing the results
System.out.println("Contains Monday: " + containsMonday);
System.out.println("Contains Sunday: " + containsSunday);
}
}
Output:
Contains Monday: true
Contains Sunday: false
Checking for Multiple Elements
You can use the contains method to check for the presence of multiple elements in an EnumSet.
Example
import java.util.EnumSet;
public class EnumSetContainsMultipleExample {
// 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);
// Checking if specific elements are present in the EnumSet
boolean containsApple = fruits.contains(Fruit.APPLE);
boolean containsMango = fruits.contains(Fruit.MANGO);
// Printing the results
System.out.println("Contains Apple: " + containsApple);
System.out.println("Contains Mango: " + containsMango);
}
}
Output:
Contains Apple: true
Contains Mango: false
Real-World Use Case
Example: Checking Selected Days of the Week
A common real-world use case for EnumSet.contains() is checking if certain days are selected in a set of days for scheduling or task management.
Example
import java.util.EnumSet;
public class SelectedDaysChecker {
// 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);
// Checking if specific days are selected
boolean isTuesdaySelected = selectedDays.contains(Day.TUESDAY);
boolean isFridaySelected = selectedDays.contains(Day.FRIDAY);
// Printing the results
System.out.println("Is Tuesday selected? " + isTuesdaySelected);
System.out.println("Is Friday selected? " + isFridaySelected);
}
}
Output:
Is Tuesday selected? false
Is Friday selected? true
In this example, EnumSet.contains() is used to check if certain days are selected, making it easy to manage and verify the presence of specific days in the set.
Conclusion
The EnumSet.contains() method in Java provides a way to check if a specified element is present in the EnumSet. By understanding how to use this method, you can efficiently manage and verify the presence of elements in 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