Java: Check if enum contains a given string?

后端 未结 29 1580
一个人的身影
一个人的身影 2020-12-02 03:59

Here\'s my problem - I\'m looking for (if it even exists) the enum equivalent of ArrayList.contains();.

Here\'s a sample of my code problem:

<         


        
29条回答
  •  生来不讨喜
    2020-12-02 04:41

    You can use this

    YourEnum {A1, A2, B1, B2}

    boolean contains(String str){ 
        return Sets.newHashSet(YourEnum.values()).contains(str);
    }                                  
    

    Update suggested by @wightwulf1944 is incorporated to make the solution more efficient.

提交回复
热议问题