Find if a String is present in an array [duplicate]
Solution 1:
This is what you're looking for:
List<String> dan = Arrays.asList("Red", "Orange", "Yellow", "Green", "Blue", "Violet", "Orange", "Blue");
boolean contains = dan.contains(say.getText());
If you have a list of not repeated values, prefer using a Set<String>
which has the same contains method
Solution 2:
String[] a= {"tube", "are", "fun"};
Arrays.asList(a).contains("any");
Solution 3:
Use Arrays.asList()
to wrap the array in a List<String>
, which does have a contains()
method:
Arrays.asList(dan).contains(say.getText())
Solution 4:
This can be done in java 8 using Stream.
import java.util.stream.Stream;
String[] stringList = {"Red", "Orange", "Yellow", "Green", "Blue", "Violet", "Orange", "Blue"};
boolean contains = Stream.of(stringList).anyMatch(x -> x.equals(say.getText());