EnumSet.of(K, K) in Java

EnumSet.of(K, K): This method is available in java.util.EnumSet class of Java.

Syntax:

<K> EnumSet<Colour> java.util.EnumSet.of(K e1, K e2)

This method takes two arguments. This method creates an enum set initially containing the specified elements.

Parameters: Two parameters are required for this method.

e1: an element that this set is to contain initially.

e2: another element that this set is to contain initially.

Returns: an enum set initially containing the specified elements.

Throws:

NullPointerException - if any parameters are null.

Approach

Java

import java.util.EnumSet;

public class EnumSetof2 {
    public enum Colour {
        RED, GREEN, YELLOW, ORANGE
    };

    public static void main(String[] args) {

        EnumSet<Colour> enumSet;

        enumSet = EnumSet.of(Colour.GREEN, Colour.ORANGE);
        System.out.println(enumSet);

    }
}

Output:

[GREEN, ORANGE]


No comments:

Post a Comment