How can I test if an array contains a certain value?
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
?solution:
Arrays.asList(yourArray).contains(yourValue)
Warning: this doesn't work for arrays of primitives (see the comments).
Since java-8
You can now use a
Stream
to check whether an array of int
, double
or long
contains a value (by respectively using a IntStream
, DoubleStream
or LongStream
)Example
int[] a = {1,2,3,4};
boolean contains = IntStream.of(a).anyMatch(x -> x == 4);
http://stackoverflow.com/questions/1128723/how-can-i-test-if-an-array-contains-a-certain-value
COMMENTS