Check If a Value is Present in an Array in Java
Last Updated :
10 Nov, 2025
Given an array of integers and a key element, the task is to check whether the key is present in the array. If the key exists, return true; otherwise, return false.
Example:
Input: arr[] = [3, 5, 7, 2, 6, 10], key = 7
Output: Is 7 present in the array: true
Input: arr[] = [-1, 1, 5, 8], key = -2
Output: Is -2 present in the array: false
1. Using Linear Search Method
In the Linear Search method, each element of the array is sequentially compared with the key until a match is found or the end of the array is reached.
Example:
Java
// Java program to check if an element is present using Linear Search
class Geeks {
private static boolean isElementPresent(int[] arr, int key) {
for (int element : arr) {
if (element == key) {
return true;
}
}
return false;
}
public static void main(String[] args) {
int[] arr = {3, 5, 7, 2, 6, 10};
int key = 7;
boolean res = isElementPresent(arr, key);
System.out.println("Is " + key + " present in the array: " + res);
}
}
OutputIs 7 present in the array: true
Explanation:
- Create a method isElementPresent which return a boolean value and takes two parameters array (arr) and the element(key).
- Iterate each element of the array and check if the element is equal to the given key return true otherwise return false.
2. Using Binary Search Method
Binary Search efficiently finds an element in a sorted array by repeatedly dividing the search interval in half.
The built-in Arrays.binarySearch() method performs this operation in logarithmic time.
Syntax:
public static int binarySearch(data_type[] arr, data_type key)
Return Value: Returns the index of the key if found; otherwise, a negative value.
Parameters:
- array: The array to be searched.
- key: The value to be searched for.
Example:
Java
// Java program to check if an element is present using Binary Search
import java.util.Arrays;
class Geeks {
private static boolean isElementPresent(int[] arr, int key) {
Arrays.sort(arr); // Binary search works only on sorted arrays
int res = Arrays.binarySearch(arr, key);
return res >= 0;
}
public static void main(String[] args) {
int[] arr = {3, 5, 7, 2, 6, 10};
int key = 17;
boolean res = isElementPresent(arr, key);
System.out.println("Is " + key + " present in the array: " + res);
}
}
OutputIs 17 present in the array: false
3. Using List.contains() Method:
The contains() method of the List interface checks if a specific element exists in a list. We can convert an array to a list using Arrays.asList().
Syntax:
public boolean contains(Object element)
Parameter: It takes a single parameter Object which to be searched in the given list.
Return Type: It return a boolean value, if the element is present in the list return true, otherwise return false.
Example:
Java
// Java program to check if an element is present using List.contains()
import java.util.Arrays;
class Geeks {
private static boolean isElementPresent(Integer[] arr, int key) {
return Arrays.asList(arr).contains(key);
}
public static void main(String[] args) {
Integer[] arr = {3, 5, 7, 2, 6, 10};
int key = 7;
boolean res = isElementPresent(arr, key);
System.out.println("Is " + key + " present in the array: " + res);
}
}
OutputIs 7 present in the array: true
4. Using Stream.anyMatch() Method
The anyMatch() method checks whether any element in a stream matches the provided predicate. It short-circuits as soon as a match is found.
Syntax:
boolean anyMatch(Predicate<T> predicate)
Parameter: This method takes a single parameter predicate of type T which is a generic.
Return Types: This method return a boolean value, True if the element is present otherwise return false.
Example 1: Using IntStream.of()
Java
// Java program to check if an element is present using anyMatch()
import java.util.stream.IntStream;
class Geeks {
private static boolean isElementPresent(int[] arr, int key) {
return IntStream.of(arr).anyMatch(x -> x == key);
}
public static void main(String[] args) {
int[] arr = {3, 5, 7, 2, 6, 10};
int key = 7;
boolean res = isElementPresent(arr, key);
System.out.println("Is " + key + " present in the array: " + res);
}
}
OutputIs 7 present in the array: true
Example 2: Using Arrays.stream() method to create Stream
Java
// Java program to check if an element is present using Arrays.stream()
import java.util.Arrays;
class Geeks {
private static boolean isElementPresent(int[] arr, int key) {
return Arrays.stream(arr).anyMatch(x -> x == key);
}
public static void main(String[] args) {
int[] arr = {3, 5, 7, 2, 6, 10};
int key = 7;
boolean res = isElementPresent(arr, key);
System.out.println("Is " + key + " present in the array: " + res);
}
}
OutputIs 7 present in the array: true
Check if a value is present in an Array in Java
Explore
Java Basics
OOP & Interfaces
Collections
Exception Handling
Java Advanced
Practice Java