Sunday 22 December 2019

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



I have a String[] with values like so:



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



Given String s, is there a good way of testing whether VALUES contains s?


Answer



Arrays.asList(yourArray).contains(yourValue)


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







Since you can now use Streams.



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


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



Example




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

No comments:

Post a Comment

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...