码迷,mamicode.com
首页 > 编程语言 > 详细

Java中EnumSet的copyOf()方法: Java.util.EnumSet.copyOf()

时间:2020-07-12 10:36:24      阅读:53      评论:0      收藏:0      [点我收藏+]

标签:type   welcome   ring   list   array   工作原理   保留   并且   strong   

[
  1. 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<GFG> collect = new ArrayList<GFG>();
     
            // 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<GFG> 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]
  2. Java中?的java.util.EnumSet.copyOf(EnumSet e_set方法用于将现有EnumSet(即e_set中的所有内容复制到新的枚举集。句法:
    New_Enum_Set = EnumSet.copyOf(EnumSet e_set

    参数:该方法接受枚举对象类型的一个参数e_set,并引用其值将被复制到New_Enum_Set的集合。

    返回值:该方法不返回任何值。

    异常:e_set为NULL?时,该方法抛出NullPointerException

    下面的程序说明了java.util.EnumSet.copyOf()方法的工作原理:

    // Java program to demonstrate copyOf() method
    import java.util.*;
    
    // Creating an enum of CARS type
    enum CARS {
    ????RANGE_ROVER,
    ????MUSTANG,
    ????CAMARO,
    ????AUDI,
    ????BMW
    }
    ;
    
    public class Enum_Set_Demo {
    
    ????public static void main(String[] args)
    ????{
    
    ????????// Creating an empty EnumSet
    ????????// Getting all elements from CARS
    ????????EnumSet<CARS> e_set = EnumSet.allOf(CARS.class);???????
    
    ????????// Displaying the initial EnumSet
    ????????System.out.println("Initial set is: " + e_set);
    
    ????????// Copying the set
    ????????EnumSet<CARS> new_set = EnumSet.copyOf(e_set);
    
    ????????// Displaying the final set
    ????????System.out.println("The new set is: " + new_set);
    ????}
    }
    

    输出:

    Initial set is: [RANGE_ROVER, MUSTANG, CAMARO, AUDI, BMW]
    The new set is: [RANGE_ROVER, MUSTANG, CAMARO, AUDI, BMW]
]
转载请保留页面地址:https://www.breakyizhan.com/java/5484.html

Java中EnumSet的copyOf()方法: Java.util.EnumSet.copyOf()

标签:type   welcome   ring   list   array   工作原理   保留   并且   strong   

原文地址:https://www.cnblogs.com/breakyizhan/p/13287207.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!