EnumSet remove(Object) in Java

remove(Object): This method is available in java.util.EnumSet class of Java.

Syntax:

boolean java.util.AbstractCollection.remove(Object o)

This method takes one argument of type Object as its parameter. This method removes a single instance of the specified element from this collection if it is present.

Parameters: One parameter is required for this method.

o: the element to be removed from this collection if present.

Returns: true if an element was removed as a result of this call.

Throws:

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

2. ClassCastException - if the type of the specified element is incompatible with this collection.

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

Approach

Java

import java.util.EnumSet;

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

    public static void main(String[] args) {

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

        enumSet.remove(Colour.GREEN);

        System.out.println(enumSet);

    }
}

Output:

[RED, YELLOW, ORANGE]


No comments:

Post a Comment