Check if Java Array contains a certain value
-
In this tutorial see various ways to check if Java Array contains a particular value.
Check if the array contains the value using linear search.public class TestArrayContains { public static void main(String args[]) { String[] values = { "AB", "BC", "CD", "AE" }; String testValue = "AB"; boolean contains = contains(values, testValue); System.out.println("Does Array contains value " + testValue + " is " + contains); } public static <T> boolean contains(final T[] array, final T v) { if (v == null) { for (final T e : array) if (e == null) return true; } else { for (final T e : array) if (e == v || v.equals(e)) return true; } return false; } }
-
Check if the array contains the value using Arrays class and Java 8 input Stream
import java.util.Arrays; public class TestArrayContains { public static void main(String args[]) { String[] values = { "AB", "BC", "CD", "AE" }; String testValue = "AB"; boolean contains = isArrayContains(values, testValue); System.out.println("Does Array contains value " + testValue + " is " + contains); } public static <T> boolean isArrayContains(T[] array, T value) { return Arrays.stream(array).anyMatch(value::equals); } }
-
Check if the array contains the value using ArrayUtils.contains from Apache Commons Lang
import java.util.Arrays; public class TestArrayContains { public static void main(String args[]) { String[] values = { "AB", "BC", "CD", "AE" }; String testValue = "AB"; if (ArrayUtils.contains(values, testValue)) { System.out.println("Array contains" + testValue); } } }
-
If the array is a int, double or long we can use these IntStream, DoubleStream or LongStream respectively
import java.util.Arrays; import java.util.stream.IntStream; public class TestArrayContains { public static void main(String args[]) { int[] values = { 1, 33, 55, 66 }; int testValue = 33; boolean contains = IntStream.of(values).anyMatch(x -> x == testValue); System.out.println("Does Array contains value " + testValue + " is " + contains); } public static <T> boolean isArrayContains(T[] array, T value) { return Arrays.stream(array).anyMatch(value::equals); } }
-
If the array is sorted we can use the binarySearch method of the Arrays class
import java.util.Arrays; import java.util.stream.IntStream; public class TestArrayContains { public static void main(String args[]) { int[] values = { 1, 33, 55, 66 }; int testValue = 33; boolean contains = IntStream.of(values).anyMatch(x -> x == testValue); System.out.println("Does Array contains value " + testValue + " is " + contains); } public static int isArrayContains(int[] array, int value) { return Arrays.binarySearch(array, value); } }
Check if the array contains the value converting it to Setimport java.util.Arrays; import java.util.HashSet; public class TestArrayContains { public static void main(String args[]) { String[] values = { "AB", "BC", "CD", "AE" }; String testValue = "AB"; System.out.println(new HashSet
(Arrays.asList(values)).contains(testValue)); } }
See Also
Spring Boot Interview Questions Apache Camel Interview Questions Drools Interview Questions Java 8 Interview Questions Enterprise Service Bus- ESB Interview Questions. JBoss Fuse Interview Questions Top ElasticSearch frequently asked interview questions