This Java example shows how to check if a given String value is present in an array. Below Java code sample uses contains method of List Interface and anyMatch method of stream API.
1 2 3 4 5 6 7 8 9 |
String[] stringArray = new String[] {"AB","BC","CD","AE"}; Arrays.asList(stringArray).contains("XY"); // --> false Arrays.asList(stringArray).contains("AB"); // --> true // Note: This doesn't work for arrays of primitives // Using Java 8 Stream String[] values = {"AB","BC","CD","AE"}; Arrays.stream(values).anyMatch("s"::equals); // --> false Arrays.stream(values).anyMatch("CD"::equals); // --> true |