Arrays.asList(yourArray).contains(yourValue)

Warning: this doesn't work for arrays of primitives (see the comments).


Since java-8 you can now use Streams.

String[] values = {"AB","BC","CD","AE"};
boolean contains = Arrays.stream(values).anyMatch("s"::equals);

To check whether an array of int, double or long contains a value use IntStream, DoubleStream or LongStream respectively.

Example

int[] a = {1,2,3,4};
boolean contains = IntStream.of(a).anyMatch(x -> x == 4);
Answer from camickr on Stack Overflow
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ java โ€บ check-if-a-value-is-present-in-an-array-in-java
Check If a Value is Present in an Array in Java - GeeksforGeeks
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(). ... Parameter: It takes a single parameter Object which to be searched in the given list.
Published: August 18, 2026
Top answer
1 of 16
3431
Arrays.asList(yourArray).contains(yourValue)

Warning: this doesn't work for arrays of primitives (see the comments).


Since java-8 you can now use Streams.

String[] values = {"AB","BC","CD","AE"};
boolean contains = Arrays.stream(values).anyMatch("s"::equals);

To check whether an array of int, double or long contains a value use IntStream, DoubleStream or LongStream respectively.

Example

int[] a = {1,2,3,4};
boolean contains = IntStream.of(a).anyMatch(x -> x == 4);
2 of 16
465

Concise update for Java SE 9

Reference arrays are bad. For this case we are after a set. Since Java SE 9 we have Set.of.

private static final Set<String> VALUES = Set.of(
    "AB","BC","CD","AE"
);

"Given String s, is there a good way of testing whether VALUES contains s?"

VALUES.contains(s)

O(1).

The right type, immutable, O(1) and concise. Beautiful.*

Original answer details

Just to clear the code up to start with. We have (corrected):

public static final String[] VALUES = new String[] {"AB","BC","CD","AE"};

This is a mutable static which FindBugs will tell you is very naughty. Do not modify statics and do not allow other code to do so also. At an absolute minimum, the field should be private:

private static final String[] VALUES = new String[] {"AB","BC","CD","AE"};

(Note, you can actually drop the new String[]; bit.)

Reference arrays are still bad and we want a set:

private static final Set<String> VALUES = new HashSet<String>(Arrays.asList(
     new String[] {"AB","BC","CD","AE"}
));

(Paranoid people, such as myself, may feel more at ease if this was wrapped in Collections.unmodifiableSet - it could then even be made public.)

(*To be a little more on brand, the collections API is predictably still missing immutable collection types and the syntax is still far too verbose, for my tastes.)

Discussions

java - Check if a value exists in an array - Stack Overflow
Hey guys I am taking a high school course for computer science and we just started our second unit arrays and I am not sure how to do a problem. So I need to make code that checks if a user inputted More on stackoverflow.com
๐ŸŒ stackoverflow.com
How to check if a value exists in an array in Java? - Stack Overflow
How to check if a value already exists in other array. like in the code below I wanted to check which values of result array are in the portOut array. I am not getting it right. More on stackoverflow.com
๐ŸŒ stackoverflow.com
Java check if array[] item exists - Stack Overflow
5 How to check if an element in array exists in Java More on stackoverflow.com
๐ŸŒ stackoverflow.com
[Java] is there a method to check if an array contains a certain value?
You can use Arrays.asList() and then the list's .contains() method. However, you're effectively doing a linear search anyway with .contains(), which uses .equals() in its comparisons. More on reddit.com
๐ŸŒ r/learnprogramming
3
1
May 29, 2014
๐ŸŒ
DigitalOcean
digitalocean.com โ€บ community โ€บ tutorials โ€บ java-array-contains-value
How to Check if a Java Array Contains a Value? | DigitalOcean
September 16, 2025 - Learn 4 proven methods to check if Java array contains value: for-loops, Streams, Arrays.asList(), and binarySearch(). Choose the right approach today!
๐ŸŒ
Baeldung
baeldung.com โ€บ home โ€บ java โ€บ java array โ€บ check if a java array contains a value
Check if a Java Array Contains a Value | Baeldung
September 7, 2024 - Weโ€™ve seen several methods of searching through an array. Based on our results, a HashSet works best for searching through a list of values. However, we need to create them in advance and store them in the Set.
๐ŸŒ
Attacomsian
attacomsian.com โ€บ blog โ€บ java-array-contains-value
How to check if an array contains a value in Java
December 1, 2019 - String[] names = {"Atta", "John", "Emma", "Tom"}; // check if `Atta` exists in list boolean exist = Arrays.stream(names).anyMatch("Atta"::equals); if(exist) { System.out.println("Hi, Atta ?"); } ... String[] names = {"Atta", "John", "Emma", "Tom"}; // convert array to list List<String> list = Arrays.asList(names); // check 'Atta' & `John` if (list.containsAll(Arrays.asList("Atta", "John"))) { System.out.println("Hi, Atta & John ?"); } ... int[] years = {2015, 2016, 2017, 2018, 2019, 2020}; // loop all elements for (int y : years) { if (y == 2019) { System.out.println("Goodbye, 2019!"); break; } }
๐ŸŒ
Vultr Docs
docs.vultr.com โ€บ java โ€บ examples โ€บ check if an array contains a given value
Java Program to Check if An Array Contains a Given Value | Vultr Docs
December 20, 2024 - public class Main { public static ... = 3; boolean found = false; for (int element : array) { if (element == valueToFind) { found = true; break; } } System.out.println("Value found: " + found); } } ... In this version, the enhanced ...
Find elsewhere
๐ŸŒ
Stack Abuse
stackabuse.com โ€บ java-check-if-array-contains-value-or-element
Java: Check if Array Contains Value or Element
November 19, 2020 - If the value is above equal to, or above 0, the array contains the element, and it doesn't contain it otherwise. The Java 8 Stream API is very versatile and offers for concise solutions to various tasks related to processing collections of objects. Using Streams for this type of task is natural and intuitive for most. Let's take a look at how we can use the Stream API to check if an array contains an integer:
๐ŸŒ
w3reference
w3reference.com โ€บ blog โ€บ how-to-check-if-a-java-array-contains-a-value
How to Check if a Java Array Contains a Value? โ€” w3reference.com
public class ArraySearch { public static boolean containsWithForLoop(int[] array, int target) { for (int i = 0; i < array.length; i++) { if (array[i] == target) { return true; } } return false; } // Example usage public static void main(String[] args) { int[] numbers = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; int searchValue = 5; boolean exists = containsWithForLoop(numbers, searchValue); System.out.println("Value " + searchValue + " exists: " + exists); // Output: Value 5 exists: true } } ... Java 5 introduced the enhanced for loop (for-each loop), which provides a more readable syntax.
๐ŸŒ
Programiz
programiz.com โ€บ java-programming โ€บ examples โ€บ array-contains-value
Java Program to Check if An Array Contains a Given Value
In the above program, instead of using a for-each loop, we convert the array to an IntStream and use its anyMatch() method. anyMatch() method takes a predicate, an expression, or a function that returns a boolean value. In our case, the predicate compares each element n in the stream to toFind ...
๐ŸŒ
LabEx
labex.io โ€บ tutorials โ€บ java-how-to-check-if-an-array-contains-a-specific-element-in-java-559997
How to Check If an Array Contains a Specific Element in Java | LabEx
Cherry was found in the array. null was found in the array. Using Objects.equals() is the recommended way to compare objects, especially when there's a possibility of one or both objects being null. This prevents unexpected NullPointerException errors. In this lab, you learned a fundamental method for checking if a specific element exists within a Java array: iterating through the array using a traditional for loop.
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ check-if-a-value-is-present-in-an-array-in-java
Check if a value is present in an Array in Java
Java Object Oriented Programming Programming ยท At first sort the array โˆ’ ยท int intArr[] = {55, 20, 10, 60, 12, 90, 59}; // sorting array Arrays.sort(intArr); Now, set the value to be searched in an int variable โˆ’ ยท int searchVal = 12; Check for the presence of a value in an array โˆ’ ยท int retVal = Arrays.binarySearch(intArr,searchVal); boolean res = retVal > 0 ? true : false; Following is an example to check if a value is present in an array โˆ’ ยท
๐ŸŒ
Quora
quora.com โ€บ How-do-you-find-out-if-an-element-exists-in-an-array-in-Java
How to find out if an element exists in an array in Java - Quora
Answer (1 of 3): This is very basic array question, you should think about it first so that your skill can improve. Array is a list of values and how do you find a element in that. For example if a bucket contains red apples and one green apple. How do you find green apple from bucket? So the pr...
๐ŸŒ
CodeSpeedy
codespeedy.com โ€บ home โ€บ check if a value exists in an array in java
Check if a value exists in an Array in Java - CodeSpeedy
September 28, 2022 - Traverse through the entire array using any loop and check each and every element of the array with the value to be found. If the value to be found got matched to any element in the array then return or print at which index the element is found ...
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ java โ€บ java-program-to-check-if-specified-element-is-present-in-the-array
Java Program to Check if Specified Element is Present in the Array - GeeksforGeeks
July 23, 2025 - The utility function takes an array and the target element as inputs and returns a boolean result. ... // Java Program to check if a specified element // is present in the array // using a utility function public class CheckElement { public static boolean isElementPresent(int[] arr, int t) { if (arr != null) { for (int n : arr) { // Match the target element if (n == t) { return true; } } } return false; } public static void main(String[] args) { int[] n1 = {1, 2, 3, 4, 5}; int t = 25; if (isElementPresent(n1, t)) { System.out.println("" + t + " is present in the array."); } else { System.out.println("" + t + " is not present in the array."); } } }
๐ŸŒ
Techie Delight
techiedelight.com โ€บ home โ€บ java โ€บ determine whether a value is present in an array in java
Determine whether a value is present in an array in Java | Techie Delight
July 7, 2026 - For sorted arrays, we can use binary search algorithm to search a value. The algorithm returns the index of the specified element and a negative value if the element is not present in the array.
๐ŸŒ
Centron
centron.de โ€บ home โ€บ tutorials โ€บ how to check if java array contains a value?
How to Check if Java Array Contains a Value? โ€“ centron
February 7, 2025 - If your array is sorted, you can use the Arrays.binarySearch() method to check if the array contains the given value or not. ... String[] vowels = { "A", "I", "E", "O", "U" }; System.out.println("Unsorted Array = " + Arrays.toString(vowels)); ...
๐ŸŒ
How to do in Java
howtodoinjava.com โ€บ home โ€บ java array โ€บ check if array contains an item in java
Check if Array Contains an Item in Java
February 3, 2023 - Also, learn to find the elementโ€™s index in the array. To check if an element is in an array, we can use Arrays class to convert the array to ArrayList and use the contains() method to check the itemโ€™s presence.