zoukankan      html  css  js  c++  java
  • Java中EnumSet的copyOf()方法: Java.util.EnumSet.copyOf()

    [
    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
  • 相关阅读:
    微软职位内部推荐-SOFTWARE ENGINEER II
    微软职位内部推荐-SOFTWARE ENGINEER II
    微软职位内部推荐-SDEII for Windows Phone Apps
    微软职位内部推荐-SDEII for Windows Phone Apps
    微软职位内部推荐-Software Engineer II-SDP
    微软职位内部推荐-SDE II
    微软职位内部推荐-Senior Software Engineer-SDP
    微软职位内部推荐-Senior Software Engineer
    微软职位内部推荐-Software Engineer II-Office Incubation
    微软职位内部推荐-Senior Software Engineer-Office Incubation
  • 原文地址:https://www.cnblogs.com/breakyizhan/p/13287207.html
Copyright © 2011-2022 走看看