How to get all enum values in Java?

后端 未结 7 1624
独厮守ぢ
独厮守ぢ 2020-12-04 17:06

I came across this problem that I without knowing the actual enum type I need to iterate its possible values.

if (value instanceof Enum){
   Enu         


        
7条回答
  •  Happy的楠姐
    2020-12-04 17:54

    One can also use the java.util.EnumSet like this

    @Test
    void test(){
        Enum aEnum =DayOfWeek.MONDAY;
        printAll(aEnum);
    }
    
    void printAll(Enum value){
        Set allValues = EnumSet.allOf(value.getClass());
        System.out.println(allValues);
    }
    

提交回复
热议问题