Home » Java programming language

Java EnumSet allOf() Method with Example

EnumSet Class allOf() method: Here, we are going to learn about the allOf() method of EnumSet Class with its syntax and example.
Submitted by Preeti Jain, on February 13, 2020

EnumSet Class allOf() method

  • allOf() method is available in java.util package.
  • allOf() method is used to return Enumset that has all of the elements of the given element type (ele_ty).
  • allOf() method is a static method, so it is accessible with the class name and if we try to access the method with the class object then we will not get an error.
  • allOf() method may throw an exception at the time of returning EnumSet object.
    NullPointerException: This exception may throw when the given parameter is null exists.

Syntax:

    public static EnumSet allOf(Class ele_ty);

Parameter(s):

  • Class ele_ty – represent the element type (ele_ty) class for this Enumset.

Return value:

The return type of this method is EnumSet, it retrieves Enumset that have set of elements for this Enum.

Example:

// Java program is to demonstrate the example of
// allOf(Class ele_ty) method of EnumSet

import java.util.*;

public class AllOfEnumSet {
    // Initialize a enum variable
    // with some constants

    public enum Colors {
        RED,
        BLUE,
        GREEN,
        PURPLE,
        YELLOW
    };

    public static void main(String[] args) {
        // Here , we are creating an empty EnumSet
        EnumSet < Colors > es = null;

        // Display EnumSet
        System.out.println("EnumSet: " + es);

        // By using allOf() method is to 
        // get all of the elements of an enum 
        // and put into an es
        es = EnumSet.allOf(Colors.class);

        // Display Modified EnumSet
        System.out.println("Updated set: " + es);
    }
}

Output

EnumSet: null
Updated set: [RED, BLUE, GREEN, PURPLE, YELLOW]



Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.