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


Warning: this doesn’t work for arrays of primitives (see the comments).

Since java-8 you can now use Streams.

String[] values = {"AB","BC","CD","AE"};
boolean contains ="s"::equals);

To check whether an array of int, double or long contains a value use IntStream, DoubleStream or LongStream respectively.


int[] a = {1,2,3,4};
boolean contains = IntStream.of(a).anyMatch(x -> x == 4);

Leave a Comment