Arrays are a fundamental data structure in Java and are widely used in a variety of applications. One of the most common operations when working with arrays is to check if the array contains a certain value. In this article, we will look at several ways to determine if an array contains a particular value in Java.

Method 1: Using the for Loop

The simplest way to check if an array contains a value is by using a for loop. You can iterate through each element of the array and compare it with the value you are looking for. If you find a match, you can return true. Here is the code:

public static boolean containsValue(int[] array, int value) {
  for (int i = 0; i < array.length; i++) {
    if (array[i] == value) {
      return true;
    }
  }
  return false;
}

Method 2: Using the Stream API

Java 8 introduced the Stream API, which provides a convenient way to perform operations on arrays. You can use the anyMatch method to check if an array contains a value. The anyMatch method returns true if there is at least one element in the stream that matches the given condition. Here is the code:

public static boolean containsValue(int[] array, int value) {
  return Arrays.stream(array).anyMatch(x -> x == value);
}

Method 3: Using the Arrays Class

The Arrays class in Java provides several useful methods for working with arrays. One of these methods is the binarySearch method. The binarySearch method returns the index of the specified value in the array if it exists. If the value does not exist, the method returns a negative number. Here is the code:

public static boolean containsValue(int[] array, int value) {
  return Arrays.binarySearch(array, value) >= 0;
}

Method 4: Using the List Interface

Another approach to checking if an array contains a value is to convert the array to a List and use the contains method. The contains method returns true if the list contains the specified element. Here is the code:

public static boolean containsValue(int[] array, int value) {
  return Arrays.asList(array).contains(value);
}

Method 5: Using HashSet

A HashSet is a collection that does not allow duplicates and can be used to check if an array contains a certain value. You can add all the elements of the array to the HashSet and then use the contains method to check if the value is present. Here is the code:

public static boolean containsValue(int[] array, int value) {
  Set set = new HashSet<>();
  for (int i : array) {
    set.add(i);
  }
  return set.contains(value);
}

Method 6: Using HashMap

A HashMap is a collection that maps keys to values and can be used to check if an array contains a certain value. You can add each element of the array as a key to the HashMap and then use the containsKey method to check if the value is present. Here is the code:

public static boolean containsValue(int[] array, int value) {
  Map<Integer, Integer> map = new HashMap<>();
  for (int i : array) {
    map.put(i, i);
  }
  return map.containsKey(value);
}

Method 7: Using Collection.frequency

The Collections class provides the frequency method which returns the number of times a specified element appears in a collection. You can use this method to check if an array contains a certain value. If the frequency of the value is greater than 0, then the array contains the value. Here is the code:

public static boolean containsValue(int[] array, int value) {
  List list = Arrays.stream(array).boxed().collect(Collectors.toList());
  return Collections.frequency(list, value) > 0;
}

Method 8: Using the Apache Commons Lang Library

If you’re using Apache Commons Lang library, you can use the ArrayUtils class to check if an array contains a certain value. The ArrayUtils class provides the contains method that returns true if the array contains the specified value. Here is the code:

import org.apache.commons.lang3.ArrayUtils;

public static boolean containsValue(int[] array, int value) {
  return ArrayUtils.contains(array, value);
}

Method 9: Using Binary Search

The Arrays class in Java provides a binarySearch method that can be used to check if an array contains a certain value. This method returns the index of the specified value if it’s present in the array. If the value is not present, it returns a negative value. Here is the code:

public static boolean containsValue(int[] array, int value) {
  int index = Arrays.binarySearch(array, value);
  return index >= 0;
}

Method 10: Using Stream API

The Stream API in Java 8 provides a functional and concise way to check if an array contains a certain value. The anyMatch method returns a boolean indicating whether any elements of a stream match a given condition. Here is the code:

public static boolean containsValue(int[] array, int value) {
  return Arrays.stream(array).anyMatch(i -> i == value);
}

Method 11: Using Java 8 Optional

The Optional class in Java 8 can be used to check if an array contains a certain value. The findFirst method returns an Optional that contains the first element in a stream that matches a given condition. You can use this method to check if the value is present in the array. Here is the code:

public static boolean containsValue(int[] array, int value) {
  return Arrays.stream(array).filter(i -> i == value).findFirst().isPresent();
}

Method 12: Using the Guava Library

If you’re using Google’s Guava library, you can use the Contains class to check if an array contains a certain value. The Contains class provides the in method that returns true if the array contains the specified value. Here is the code:

import com.google.common.base.Contains;

public static boolean containsValue(int[] array, int value) {
  return Contains.in(value, array);
}

Conclusion

In conclusion, there are several different approaches to check if an array contains a certain value in Java. Each method has its own advantages and trade-offs, so you should choose the one that best fits your specific needs and requirements. Whether you prefer a simple for loop or a more sophisticated method using the Java 8 Stream API or the Guava library, these methods will help you solve this common problem with ease.

Leave a Reply