EnumSet add(K) in Java

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

Syntax:

boolean java.util.AbstractCollection.add(K)

This method takes one argument. This method ensures that this collection contains the specified element.

Note: Returns true if this collection changed as a result of the call.

Parameters: One parameter is required for this method.

e: element whose presence in this collection is to be ensured.

Returns: true if this collection changed as a result of the call.

Throws:

1. UnsupportedOperationException - if the add operation is not supported by this collection.

2. ClassCastException - if the class of the specified element prevents it from being added to this collection.

3. NullPointerException - if the specified element is null and this collection does not permit null elements.

4. IllegalArgumentException -If some property of the element prevents it from being added to this collection.

5. IllegalStateException - If the element cannot be added at this time due to insertion restrictions

Approach

Java

import java.util.EnumSet;

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

    public static void main(String[] args) {

        EnumSet<Colour> enumSet = EnumSet.allOf(Colour.class);

        Colour colour = Colour.valueOf("ORANGE");
        System.out.println(enumSet.add(colour));
    }
}

Output:

false


No comments:

Post a Comment