How can I check if an element exists in a Set of items?

后端 未结 4 661
无人及你
无人及你 2021-02-06 21:57

In an if statement in Java how can I check whether an object exists in a set of items. E.g. In this scenario i need to validate that the fruit will be an apple, ora

4条回答
  •  暖寄归人
    2021-02-06 22:45

    for completeness using google-collections/guava:

    import com.google.common.collect.Sets;
    
    static final Set fruit = Sets.newHashSet("APPLE", "ORANGES", "GRAPES");
    
    if (fruit.contains(fruitname))
    

    or using the plane old jdk classes:

    static final Set fruit = new HashSet(Arrays.asList("APPLE", "ORANGES", "GRAPES"));
    

提交回复
热议问题