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...
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?
29 Answers
Arrays.asList(yourArray).contains(yourValue)
Warning: this doesn't work for arrays of primitives (see the comments).
Must Read
Since java-8 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.