枚举集是集合接口的实现,它扩展了java中的枚举类型的抽象类。 are Set 集合提供了在 java 中使用枚举的功能。枚举集不同步;因此它们提供了更快的元素访问。枚举集不允许添加空元素;如果我们尝试添加空值,则会抛出空指针异常。
语法:
开始您的免费软件开发课程
网络开发、编程语言、软件测试及其他
public abstract class EnumSet<E extends Enum<E>> || This is the class decralation of enum set. EnumSet<E extends Enum<E>> enum1, enum2, enum3, enum4; || Declare different enum.
由于枚举集不是同步的,所以它们很掌握,但我们可以通过使用集合类的synchronized方法()来同步它们,下面找到synchronized的语法。
Collections.synchronizedSet(java.util.Set<T>) Set<Enum_Type> s = Collections.synchronizedSet(EnumSet.noneOf(EnumClass.class));
注意:
通过枚举集实现的接口:
通过枚举集扩展的类如下:
以下是枚举集的示例:
在下面的示例中,w 将元素添加到枚举集。
代码:
import java.util.EnumSet; public class EnumsetDemo { public static void main(String[] args) { // creating reference EnumSet<EnumDemo> enum1; // here we are adding elements to the enum1 which we create above. enum1 = EnumSet.of(EnumDemo.JAVA, EnumDemo.JAVASCRIPT, EnumDemo.C, EnumDemo.PHP, EnumDemo.GO, EnumDemo.PYTHON, EnumDemo.TYPESCRIPT); System.out.println("Enum set 1 containing values are :: "); System.out.println( enum1); } } enum EnumDemo { JAVA, PYTHON, PHP, C, GO, JAVASCRIPT, TYPESCRIPT };
输出:
在此示例中,我们仅使用值 sf enum1 创建 enum2。下面是一个例子。
代码:
import java.util.EnumSet; public class EnumsetDemo { public static void main(String[] args) { // creating reference EnumSet<EnumDemo> enum1; // here we are adding elements to the enum1 which we create above. enum1 = EnumSet.of(EnumDemo.JAVA, EnumDemo.JAVASCRIPT, EnumDemo.C, EnumDemo.PHP, EnumDemo.GO, EnumDemo.PYTHON, EnumDemo.TYPESCRIPT); System.out.println("Enum set 1 containing values are :: "); System.out.println( enum1); // adding value to another enum set using addAll method EnumSet<EnumDemo> enum2 = EnumSet.allOf(EnumDemo.class); System.out.println("Enum set 2 containing values are :: "); System.out.println( enum2); } } enum EnumDemo { JAVA, PYTHON, PHP, C, GO, JAVASCRIPT, TYPESCRIPT };
输出:
在此示例中,我们正在查找 f enum1 到 enum2 的补集。
代码:
import java.util.EnumSet; public class EnumsetDemo { public static void main(String[] args) { // creating reference EnumSet<EnumDemo> enum1; // here we are adding elements to the enum1 which we create above. enum1 = EnumSet.of(EnumDemo.JAVA, EnumDemo.JAVASCRIPT, EnumDemo.C, EnumDemo.PHP, EnumDemo.GO, EnumDemo.PYTHON, EnumDemo.TYPESCRIPT); System.out.println("Enum set 1 containing values are :: "); System.out.println( enum1); // finind completents of enum1 to enum2 EnumSet<EnumDemo> enum2 = EnumSet.complementOf(enum1); System.out.println("Enum set 2 containing values are :: "); System.out.println( enum2); } } enum EnumDemo { JAVA, PYTHON, PHP, C, GO, JAVASCRIPT, TYPESCRIPT };
输出:
Specifying range to elements to be copied.
Code:
import java.util.EnumSet; public class EnumsetDemo { public static void main(String[] args) { // creating reference EnumSet<EnumDemo> enum1; // here we are adding elements to the enum1 which we create above. enum1 = EnumSet.of(EnumDemo.JAVA, EnumDemo.JAVASCRIPT, EnumDemo.C, EnumDemo.PHP, EnumDemo.GO, EnumDemo.PYTHON, EnumDemo.TYPESCRIPT); System.out.println("Enum set 1 containing values are :: "); System.out.println( enum1); // here we are specifing the range to another enum using enum1 EnumSet<EnumDemo> enum2 = EnumSet.range(EnumDemo.PYTHON, EnumDemo.JAVASCRIPT); System.out.println("Enum set 2 containing values are :: "); System.out.println( enum2); } } enum EnumDemo { JAVA, PYTHON, PHP, C, GO, JAVASCRIPT, TYPESCRIPT };
Output:
Making copy from existing enum.
Code:
import java.util.EnumSet; public class EnumsetDemo { public static void main(String[] args) { // creating reference EnumSet<EnumDemo> enum1; // here we are adding elements to the enum1 which we create above. enum1 = EnumSet.of(EnumDemo.JAVA, EnumDemo.JAVASCRIPT, EnumDemo.C, EnumDemo.PHP, EnumDemo.GO, EnumDemo.PYTHON, EnumDemo.TYPESCRIPT); System.out.println("Enum set 1 containing values are :: "); System.out.println( enum1); // here we are copying elements of enum1 to enum2. EnumSet<EnumDemo> enum2 = EnumSet.copyOf(enum1); System.out.println("Enum set 2 containing values are :: "); System.out.println( enum2); } } enum EnumDemo { JAVA, PYTHON, PHP, C, GO, JAVASCRIPT, TYPESCRIPT };
Output:
Java EnumSet is the implementation of the SET interface in java. They are a special type of java collection framework that provides support for enum types in java. We can also make them synchronized as these are not synchronized.
以上是Java 中的枚举集的详细内容。更多信息请关注PHP中文网其他相关文章!