Arrays.asList(yourArray).contains(yourValue)
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 = 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);
Answer from camickr on Stack OverflowArrays.asList(yourArray).contains(yourValue)
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 = 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);
Concise update for Java SE 9
Reference arrays are bad. For this case we are after a set. Since Java SE 9 we have Set.of.
private static final Set<String> VALUES = Set.of(
"AB","BC","CD","AE"
);
"Given String s, is there a good way of testing whether VALUES contains s?"
VALUES.contains(s)
O(1).
The right type, immutable, O(1) and concise. Beautiful.*
Original answer details
Just to clear the code up to start with. We have (corrected):
public static final String[] VALUES = new String[] {"AB","BC","CD","AE"};
This is a mutable static which FindBugs will tell you is very naughty. Do not modify statics and do not allow other code to do so also. At an absolute minimum, the field should be private:
private static final String[] VALUES = new String[] {"AB","BC","CD","AE"};
(Note, you can actually drop the new String[]; bit.)
Reference arrays are still bad and we want a set:
private static final Set<String> VALUES = new HashSet<String>(Arrays.asList(
new String[] {"AB","BC","CD","AE"}
));
(Paranoid people, such as myself, may feel more at ease if this was wrapped in Collections.unmodifiableSet - it could then even be made public.)
(*To be a little more on brand, the collections API is predictably still missing immutable collection types and the syntax is still far too verbose, for my tastes.)
[Java] is there a method to check if an array contains a certain value?
[Java] Implementing an ArrayList and the contains method and indexOf methods are returning wrong values.
Parameter 0 of function 'array_contains()' requires an array type, but argument is of type 'java.lang.String[]'
How do you check if a java array contains a certain element?
Videos
Eg: I have int[] b, with 100 elements and I want to check if any element is of value 2. Is there an b.contains(2) method which will return true if b contains 2, or do I have to do a linear search through the array, and return false at the end if no 2 is found? Thanks!
So I am working on implementing an arrayList for a class of mine and I'm having problems getting the indexOf and contains methods working. For example I run this code to test
System.out.println("Location of \"c\" (should be 2): " + myList.indexOf("c"));
System.out.println("Location of \"z\" (should be -1): " + myList.indexOf("z"));
System.out.println("Contains \"a\" (should be true): " + myList.contains("a")); but when I run it this is what is printed out:
Location of "c" (should be 2): -1 Location of "z" (should be -1): -1 Contains "a" (should be true): false
Which means something must be wrong with my indexOf method and my contains method. My indexOf methods use contains so I think my contains is the source of the problem
For my contains method the code looks like this
public boolean contains(T o) {
for (int i = 0;i < size; i++){
if (array[i].equals(o))
return true;
else
i++;
}
return false;
}I'm trying to find if the list contains the object but it consistently returns false.
While I think it's the contains method that's screwing up I'm not certain if my indexOf method might be screwing it up too and the code for that one is here:
public int indexOf(T o) {
if(contains(o).equals(false))
return -1;
for (int i = 0; i < size ; i++) {
if (get(i).equals(o)) {
return i;
}
}
return -1;
}I can't figure out why the values I want aren't coming through correctly and any advice would be appreciated