Java: Check if enum contains a given string?

后端 未结 29 1568
一个人的身影
一个人的身影 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:36

    Guavas Enums could be your friend

    Like e.g. this:

    enum MyData {
        ONE,
        TWO
    }
    
    @Test
    public void test() {
    
        if (!Enums.getIfPresent(MyData.class, "THREE").isPresent()) {
            System.out.println("THREE is not here");
        }
    }
    

提交回复
热议问题