Home » Java programming language

Java EnumSet noneOf() Method with Example

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

EnumSet Class noneOf() method

  • noneOf() method is available in java.util package.
  • noneOf() method is used to creates an empty enum set with the given element type (ele_ty).
  • noneOf() method is a static method, it is accessible with classname and if we try to access the method with class object then we will not get an error.
  • noneOf() method may throw an exception at the time of creating blank enum set.
    NullPointerException: This exception may throw when the given parameter is null exists.

Syntax:

    public static EnumSet noneOf(Class ele_ty);

Parameter(s):

  • Class ele_ty – represents the Class object of the element type for this EnumSet.

Return value:

The return type of this method is EnumSet, it returns nothing.

Example:

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

import java.util.*;

public class NoneOfEnumSet {
    // 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 two EnumSet
        EnumSet < Colors > all_of = null;

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

        // Display Modified EnumSet
        System.out.println("EnumSet.allOf(Colors.class): " + all_of);

        // By using noneOf() method is to 
        // get none of the elements exists
        // in an EnumSet none_of
        EnumSet none_of = EnumSet.noneOf(Colors.class);

        // Display Modified EnumSet
        System.out.println("EnumSet.noneOf(Colors.class): " + none_of);
    }
}

Output

EnumSet.allOf(Colors.class): [RED, BLUE, GREEN, PURPLE, YELLOW]
EnumSet.noneOf(Colors.class): []


Comments and Discussions!

Load comments ↻





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