枚舉集是集合介面的實現,它擴展了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中文網其他相關文章!