How to get all enum values in Java?

后端 未结 7 1627
独厮守ぢ
独厮守ぢ 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条回答
  •  情话喂你
    2020-12-04 18:01

    Here, Role is an enum which contains the following values [ADMIN, USER, OTHER].

    List roleList = Arrays.asList(Role.values());
    roleList.forEach(role -> {
        System.out.println(role);
        });
    

提交回复
热议问题