Home  >  Core Java

Example of EnumSet in Java

By Arvind Rai, May 31, 2013
EnumSet is a Set in Java to store enum. Enum must be of one enum type. Null elements are not permitted. EnumSet can be synchronized as below.
 Set<TestEnum> s = Collections.synchronizedSet(EnumSet.noneOf(TestEnum.class));
 
EnumSet.noneOf creates an empty EnumSet.
EnumSetDemo.java
package com.concretepage.util;
import java.util.EnumSet;
import java.util.Iterator;
public class EnumSetDemo {
    public static void main(String[] args) {
        EnumSet<TestEnum> enSet = EnumSet.of(TestEnum.A, TestEnum.B, TestEnum.C, TestEnum.D);
        Iterator<TestEnum> ite = enSet.iterator();
        while(ite.hasNext()){
            System.out.println(ite.next());
        }
    }
}
enum TestEnum {
    A, B, C, D;
}
POSTED BY
ARVIND RAI
ARVIND RAI
FIND MORE TUTORILAS






©2019 concretepage.com | Privacy Policy | Contact Us