EnumSet addAll(Collection) in Java

addAll(Collection): This method is available in java.util.EnumSet class of Java.

Syntax:

boolean java.util.AbstractCollection.addAll(Collection<? extends K> c)

This method takes one argument. This method adds all of the elements in the specified collection to this collection.

Parameters: One parameter is required for this method.

c: collection containing elements to be added to this collection.

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

Throws:

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

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

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

4. IllegalArgumentException - if some property of an element of the specified collection prevents it from being added to thiscollection.

5. IllegalStateException - if not all the elements can be added at this time due to insertion restrictions.

Approach

Java

import java.util.EnumSet;

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

    public static void main(String[] args) {

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

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

        System.out.println(enumSet.addAll(enumSet1));

    }
}

Output:

true


No comments:

Post a Comment