How do I determine whether an array contains a particular value in Java?

后端 未结 29 2745
予麋鹿
予麋鹿 2020-11-21 05:00

I have a String[] with values like so:

public static final String[] VALUES = new String[] {\"AB\",\"BC\",\"CD\",\"AE\"};

Given

29条回答
  •  情书的邮戳
    2020-11-21 05:34

    Try using Java 8 predicate test method

    Here is a full example of it.

    import java.util.Arrays;
    import java.util.List;
    import java.util.function.Predicate;
    public class Test {
     public static final List VALUES = Arrays.asList("AA", "AB", "BC", "CD", "AE");
    
     public static void main(String args[]) {
      Predicate containsLetterA = VALUES -> VALUES.contains("AB");
      for (String i : VALUES) {
    
       System.out.println(containsLetterA.test(i));
      } 
     }
    }
    

    http://mytechnologythought.blogspot.com/2019/10/java-8-predicate-test-method-example.html

    https://github.com/VipulGulhane1/java8/blob/master/Test.java

提交回复
热议问题