JavaDevNotes.com

Check If Java Array Contains A Certain Value

Arrays are very easy to use to hold large amount of values or Object instances. But a common problem is checking if a given array contains a specific value. There are several ways of doing this, some are efficient and some are not. Below are some examples on how to test if a Java array contains a certain value.

Check Array Contains Using For Loop

The easiest method that comes to mind for searching if an array contains a specific value is by coding it using for loop. Here is an example:

public class Test {
   public static boolean contains(int[] arr, int item) {
      for (int n : arr) {
         if (item == n) {
            return true;
         }
      }
      return false;
   }
   public static void main(String[] args) {
      int[] myArray = { 5, 2, 17, 13, 12, 19, 7, 3, 9, 15 };
      System.out.println(contains(myArray, 13));
      System.out.println(contains(myArray, 25));
   }
}

The for loop is used to go through each item of the array one by one and compare. True is returned if one matches the item, otherwise false is returned. The output of the code will be:
true
false
Because 13 is in the given array, while 25 is not. Note that this is not a very efficient solution since the code may potentially compare all items in the array.

Check Array Contains Using Arrays.binarySearch

The Arrays.binarySearch can also be used to check if a given value exists in an array. This is a more efficient solution that the code above because the number of comparison is minimal. The benefit of binary search becomes more apparent when the size of the array becomes larger. You may check the explanation of binary search in Wikipedia. Below is an example of how to check is an array contains a value using binary search:

import java.util.Arrays;
public class Test {
   public static boolean contains(int[] arr, int item) {
      int index = Arrays.binarySearch(arr, item);
      return index >= 0;
   }
   public static void main(String[] args) {
      int[] myArray = { 5, 2, 17, 13, 12, 19, 7, 3, 9, 15 };
      Arrays.sort(myArray);
      System.out.println(contains(myArray, 13));
      System.out.println(contains(myArray, 25));
   }
}

The binary search gives the index of the item in the array. If the index is negative, it means the item is not found. Otherwise, it means it exist in the array. Note that binary search have one big assumption. The array is ASSUMED TO BE SORTED for it to return a correct result. Hence the code "Arrays.sort(myArray);" is very important prior to calling binary search. If we ommit this, the result will be unpredictable. For example:
import java.util.Arrays;
public class Test {
   public static boolean contains(int[] arr, int item) {
      int index = Arrays.binarySearch(arr, item);
      return index >= 0;
   }
   public static void main(String[] args) {
      int[] myArray = { 5, 2, 17, 13, 12, 19, 7, 3, 9, 15 };
      System.out.println(contains(myArray, 13));
      System.out.println(contains(myArray, 25));
   }
}

The call to sort was taken out and the output will not be correct. The code above will display:

false
false

Which is not correct.

Check Array Contains Using List

A List has a built-in method to check if it contains a specific value. We can use this to test if an Java array contains a value. First is we need to convert the array to List, and then invoke the contains method of the List. Here is an example code:
import java.util.Arrays;
import java.util.List;
public class Test {
   public static boolean contains(Integer[] arr, Integer item) {
      List<Integer> list = Arrays.asList(arr);
      return list.contains(item);
   }
   public static void main(String[] args) {
      Integer[] myArray = { 5, 2, 17, 13, 12, 19, 7, 3, 9, 15 };
      System.out.println(contains(myArray, 13));
      System.out.println(contains(myArray, 25));
   }
}

Check Array Contains Using Set

Similar to List, a Set also have the method contains to check if it has a certain value. We can also use this for checking if an array contains a certain value. For example:

import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
public class Test {
   public static boolean contains(Integer[] arr, Integer item) {
      List<Integer> list = Arrays.asList(arr);
      Set<Integer> set = new HashSet<Integer>(list);
      return set.contains(item);
   }
   public static void main(String[] args) {
      Integer[] myArray = { 5, 2, 17, 13, 12, 19, 7, 3, 9, 15 };
      System.out.println(contains(myArray, 13));
      System.out.println(contains(myArray, 25));
   }
}

A Set is conceptually more efficient than List because it does not contain duplicate values. It should use fewer comparison than a List.

Check Array Contains Using Stream of Java 8

If you are using Java 8 or higher, using stream is another option for finding if an item exist in the array. Here is a sample code:

public class Test {
   public static boolean contains(Integer[] arr, Integer item) {
      return Arrays.stream(arr).anyMatch(item::equals);
   }
   public static void main(String[] args) {
      Integer[] myArray = { 5, 2, 17, 13, 12, 19, 7, 3, 9, 15 };
      System.out.println(contains(myArray, 13));
      System.out.println(contains(myArray, 25));
   }
}