Check if value exists in list of objects Java 8

This post will discuss how to check if a primitive or an object array contains a particular value or not in Java.

1. Linear search

A naive solution is to perform a linear search on the given array to check if a particular value is present in the array or not.

⮚ For primitive arrays

1
2
3
4
5
6
7
8
9
10
11
// Method to check if a primitive array contains a particular value
public static boolean isPresent[int[] a, int target]
{
for [int i: a]
{
if [target == i] {
return true;
}
}
return false;
}

⮚ For object arrays

1
2
3
4
5
6
7
8
9
10
11
// Generic method to check if an object array contains a particular value
public static boolean isPresent[T[] a, T target]
{
for [T s: a]
{
if [target.equals[s]] {
return true;
}
}
return false;
}

2. Using Java 8 Stream

We can use Java 8 Stream to check if an array contains a particular value, as shown below:

⮚ For primitive arrays

1
2
3
4
5
6
// Method to check if a primitive array contains a particular value
public static boolean isPresent[int[] a, int target]
{
return Arrays.stream[a]
.anyMatch[x -> x == target];
}

Or using filters:

1
2
3
4
5
6
7
// Method to check if a primitive array contains a particular value
public static boolean isPresent[int[] a, int target]
{
return Arrays.stream[a]
.filter[x -> x == target]
.count[] > 0;
}

⮚ For object arrays

1
2
3
4
5
6
// Generic method to check if an object array contains a particular value
public static boolean isPresent[T[] a, T target]
{
return Arrays.stream[a]
.anyMatch[x -> target.equals[x]];// or use `target::equals`
}

Or using filters:

1
2
3
4
5
6
7
// Generic method to check if an object array contains a particular value
public static boolean isPresent[T[] a, T target]
{
return Arrays.stream[a]
.filter[x -> target.equals[x]]// or use `target::equals`
.count[] > 0;
}

3. Convert to set or List

The idea is to wrap the given array in a list and use List.contains[] that returns true if this list contains the specified element. We can also use set in place of List.

⮚ For primitive arrays

Check how to convert a primitive primitive integer array to a list?

1
2
3
4
5
6
7
8
// Method to check if a primitive array contains a particular value
public static boolean isPresent[int[] a, int target]
{
return Arrays.stream[a] // IntStream
.boxed[]// Stream
.collect[Collectors.toList[]]// List
.contains[target];
}

⮚ For object arrays

1
2
3
4
// Generic method to check if an object array contains a particular value
public static boolean isPresent[T[] a, T target] {
return Arrays.asList[a].contains[target];
}

4. Binary Search Algorithm

For sorted arrays, we can use binary search to search the array for the particular value. The binary search algorithm returns the index of the specified element is present in the array; otherwise, it returns a negative value. We can determine whether the array contains an element by simply checking if the computed index is greater or equal to zero.

⮚ For primitive arrays

1
2
3
4
// Method to check if a primitive array contains a particular value
public static int find[int[] a, int target] {
return Arrays.binarySearch[a, target] >= 0;
}

⮚ For object arrays

1
2
3
4
// Generic method to check if an object array contains a particular value
public static int find[T[] a, T target] {
return Arrays.binarySearch[a, target] >= 0;
}

5. Using Apache Commons Lang

Apache Commons Lang ArrayUtils class contains several static utility methods that operate on primitive or object arrays. It provides a contains[] method that checks if the given value is present in the array or not.

⮚ For primitive arrays

1
2
3
4
// Method to check if a primitive array contains a particular value
public static boolean isPresent[int[] a, int target] {
return ArrayUtils.contains[a, target];
}

⮚ For object arrays

1
2
3
4
// Generic method to check if an object array contains a particular value
public static boolean isPresent[T[] a, T target] {
return ArrayUtils.contains[a, target];
}

6. Using Guava Library

⮚ For primitive arrays

Guava library provides several utility classes pertaining to primitives, like Ints for int, Longs for long, Doubles for double, Floats for float, Booleans for boolean, and so on.

Each utility class has an indexOf[] method that returns the index of the first appearance of the target in the array. We can determine whether the array contains an element by checking if the returned index is greater or equal to zero.

1
2
3
4
// Method to check if a primitive array contains a particular value
public static boolean find[int[] a, int target] {
return Ints.indexOf[a, target] >= 0;
}

⮚ For object arrays

Guavas Iterables class contains a static utility method indexOf[Iterator, Predicate] that returns the index of the first element that satisfies the provided predicate, or -1 if the iterator has no such elements.

1
2
3
4
5
6
7
// Generic method to check if an object array contains a particular value
public static boolean find[T[] a, T target]
{
int index = Iterators.indexOf[Iterators.forArray[a],
Predicates.in[Collections.singleton[target]]];
return index >= 0;
}


For Java 8 and above, we can use lambda expressions:

1
2
3
4
// Generic method to check if an object array contains a particular value
public static boolean find[T[] a, T target] {
return Iterators.indexOf[Iterators.forArray[a], x -> x.equals[target]] >= 0;
}

Thats all about determining whether an array contains a particular value in Java.

Video liên quan

Chủ Đề