Java中的java.util.EnumSet.copyOf(Collection collect)方法用于将集合中的所有内容复制到新的枚举集。首先,集合由枚举的元素组成,然后创建一个新的枚举集,它是集合的副本。句法:
New_Enum_Set = EnumSet.copyOf(Collection collect)
参数:该方法接受枚举对象类型的一个参数collect,并引用其值将被复制到New_Enum_Set的集合。
返回值:该方法不返回任何值。
例外:
IllegalArgumentException:如果collect不是EnumSet实例并且包含无法与枚举进行比较或不包含元素的元素,则抛出此异常。
NullPointerException:如果collect为NULL,则抛出此异常。
下面的程序说明了java.util.EnumSet.copyOf()方法的工作原理
// Java program to demonstrate copyOf() method
import java.util.*;
// Creating an enum of GFG type
enum GFG {
Welcome,
To,
The,
World,
of,
Geeks
}
;
public class Enum_Set_Demo {
public static void main(String[] args)
{
// Creating an empty collection
Collection collect = new ArrayList();
// Adding elements to the Collection
collect.add(GFG.Welcome);
collect.add(GFG.World);
collect.add(GFG.Geeks);
// Displaying the collection
System.out.println("The collection is: " + collect);
EnumSet e_set = EnumSet.copyOf(collect);
// Displaying the final set
System.out.println("The enum set is:" + e_set);
}
}
输出:
The collection is: [Welcome, World, Geeks]
The enum set is:[Welcome, World, Geeks]