Check if array contains value Java example shows how to check if array contains a value in Java using various approaches.
How to check if an array contains a value in Java?
Let’s first create an array as given below. We are going to use a string array for our example.
1 2 3 4 5 6 7 |
String[] strArray = new String[]{ "Sunday", "Monday", "Tuesday", "Thursday", "Friday" }; |
There are several ways using which we can check if the above array contains a specific value as given below.
1) Using for loop
The simplest way to search an element in an array is to loop through the array and compare the value like given below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
package com.javacodeexamples.basic.arrayexamples; public class ArrayContainsValueExample { public static void main(String[] args) { String[] strArray = new String[]{ "Sunday", "Monday", "Tuesday", "Thursday", "Friday" }; //element value we want to search in array String strSearchValue = "Monday"; boolean found = contains(strArray, strSearchValue); System.out.println( "Array contains " + strSearchValue + ": " + found ); } private static <T> boolean contains(T[] array, T searchValue) { if( searchValue != null ){ for(T value : array){ if( value == searchValue || value.equals(searchValue)){ return true; } } } //not found return false; } } |
Output
1 |
Array contains Monday: true |
The contains
method defined above uses generics and can be used with any object type. However, it does not work with the primitive array. In the case of primitive value, use a specific implementation of the contains
method.
2) Using the asList and contains methods of List
We can convert array to List object using the asList
method and then use the contains
method of the List to search value in array as given below.
1 2 3 4 |
String strSearchValue = "Thursday"; boolean found = Arrays.asList(strArray).contains(strSearchValue); System.out.println( "Array contains " + strSearchValue + ": " + found ); |
Output
1 |
Array contains Thursday: true |
Note: This approach does not work with an array of primitives.
3) Using the sort and binarySearch methods of the Arrays class
Above given approaches are useful if you want to do a few searches in an array. If the requirement is to search value in an array again and again, it is better to sort the array once and then use the binary search multiple times to check if the array contains given values.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
//sort the array using sort method of Arrays class Arrays.sort(strArray); String strSearchValue = "Saturday"; /* * use binarySearch method of Arrays class to search an element in array * it returns index of the search value in array */ int found = Arrays.binarySearch(strArray, strSearchValue); if( found > 0 ) System.out.println("Array contains " + strSearchValue ); else System.out.println("Array does not contain " + strSearchValue ); |
This approach performs better when we want to search value in the same array lot many times.
4) Using the Apache Commons
If you are using the Apache Commons library, you can use the contains
method of the ArrayUtils class to search elements in an array as given below.
1 2 3 4 5 |
String strSearchValue = "Tuesday"; boolean found = ArrayUtils.contains(strArray, strSearchValue); System.out.println( "Array contains " + strSearchValue + ": " + found ); |
Output
1 |
Array contains Tuesday: true |
5) Using Java 8 Stream
If you are using version Java 8, you can use the Stream to search elements in an array as given below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
public class ArrayContainsValueExample { public static void main(String[] args) { String[] strArray = new String[]{ "Sunday", "Monday", "Tuesday", "Thursday", "Friday" }; //value to search in array String strSearchValue = "Thursday"; boolean found = contains(strArray, strSearchValue); System.out.println( "Array contains " + strSearchValue + ": " + found ); } private static <T> boolean contains(T[] array, T searchValue){ return Arrays.stream(array).anyMatch(searchValue::equals); } } |
Output
1 |
Array contains Thursday: true |
6) Using the HashSet class
The HashSet class provides constant-time performance for contains operation. You can convert an array to HashSet and then use its contains
method to search an element in the array as given below.
1 2 3 4 5 6 7 8 9 |
//convert array to HashSet HashSet<String> setDays = new HashSet<String>( Arrays.asList(strArray) ); String strSearchValue = "Friday"; //use contains method of HashSet to search array boolean found = setDays.contains(strSearchValue); System.out.println( "Array contains " + strSearchValue + ": " + found ); |
Output
1 |
Array contains Friday: true |
Since the HashSet provides constant-time performance for contains operation, this approach gives better performance if you want to search the same array many times.
This example is a part of the Java String tutorial with examples and the Java Array tutorial with examples.
Please let me know your views in the comments section below.