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

Ad: Are you a blogger? Join our Blogging forum.





Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.