Home » Java programming language

Java EnumSet copyOf() Method with Example

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

EnumSet Class copyOf() method

Syntax:

    public static EnumSet copyOf(Collection co);
    public static EnumSet copyOf(EnumSet es);
  • copyOf() method is available in java.util package.
  • copyOf(Collection co) method is used to return an EnumSet from the given Collection.
  • copyOf(EnumSet es) method is used to return the EnumSet from the given EnumSet or in other words we can say it creates an enum set & assign all the copied element from the given enum set.
  • copyOf(Collection co) method may throw an exception at the time of copying element.
    • IllegalArgumentException: This exception may throw when the given parameter co is not an EnumSet object.
    • NullPointerException: This exception may throw when the given parameter is null exists.
  • copyOf(EnumSet es) method may throw an exception at the time of copying EnumSet elements.
    • NullPointerException: This exception may throw when the given parameter is null exists
  • These are static methods, it is accessible with the class name and if we try to access these methods with the class object then also we will not get any error.

Parameter(s):

  • In the first case, copyOf(Collection co),
    • Collection co – represents the Collection object from which to copy this enum set.
  • In the first case, copyOf(EnumSet es),
    • EnumSet es – represents the EnumSet object from which to copy this enum set.

Return value:

The return type of this method is EnumSet, it does not return anything.

Example:

// Java program is to demonstrate the example of
// copyOf f() method of EnumSet

import java.util.*;

public class CopyOfEnumSet {
    // Initialize a enum variable
    // with some constants
    public enum Colors {
        RED,
        BLUE,
        GREEN,
        PURPLE,
        YELLOW
    };

    public static void main(String[] args) {

        Collection co = new LinkedList();
        EnumSet es = EnumSet.allOf(Colors.class);

        // By using add() method is to add
        // the elements in a linked list

        co.add(Colors.RED);
        co.add(Colors.BLUE);
        co.add(Colors.GREEN);
        co.add(Colors.PURPLE);

        // By using copyOf(collection) method is to
        // copy the elements of the given collection
        // into an EnumSet
        EnumSet es1 = EnumSet.copyOf(co);

        // Display EnumSet
        System.out.println("EnumSet.copyOf(co): " + es1);

        // By using copyOf(EnumSet) method is to
        // copy the elements of the given EnumSet
        // into an another EnumSet
        es1 = EnumSet.copyOf(es);

        // Display EnumSet
        System.out.println("EnumSet.copyOf(es): " + es1);
    }
}

Output

EnumSet.copyOf(co): [RED, BLUE, GREEN, PURPLE]
EnumSet.copyOf(es): [RED, BLUE, GREEN, PURPLE, YELLOW]



Comments and Discussions!

Load comments ↻






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