Java EnumSet contains() Method

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

  1. Introduction
  2. contains Method Syntax
  3. Examples
    • Basic Usage of contains Method
    • Checking for Multiple Elements
  4. Real-World Use Case
    • Example: Checking Selected Days of the Week
  5. 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: true if 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.

Comments