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
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.)

🌐
W3Schools
w3schools.com › JAVA › ref_arraylist_contains.asp
Java ArrayList contains() Method
The contains() method returns true if an item exists in a list and false otherwise. ... Coding fundamentals as bite-sized lessons and challenges. ... If you want to use W3Schools services as an educational institution, team or enterprise, send ...
🌐
Lucee Documentation
docs.lucee.org › reference › functions › arraycontains()
array.contains() :: Lucee Documentation
Returns the position of the first element in the array that matches the item that we are searching for, or 0 if the item is not found.
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › util › Arrays.html
Arrays (Java Platform SE 8 )
July 21, 2026 - Searches the specified array of ints for the specified value using the binary search algorithm. The array must be sorted (as by the sort(int[]) method) prior to making this call. If it is not sorted, the results are undefined. If the array contains multiple elements with the specified value, there is no guarantee which one will be found.
🌐
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 - Three commonly used methods for searching an array are as a List, a Set, or with a loop that examines each member until it finds a match.
🌐
Career Karma
careerkarma.com › blog › java › java array contains
Java Array Contains: The Complete Guide | Career Karma
December 1, 2023 - There are a few approaches that you can use to check whether an array contains a particular value. This tutorial will discuss, with examples, how you can check—using a for-each loop, contains(), and stream()—if an array in your Java code contains a certain value.
Find elsewhere
🌐
GeeksforGeeks
geeksforgeeks.org › java › arraylist-contains-java
Arraylist.contains() Method in Java - GeeksforGeeks
July 18, 2026 - The contains() method returns false for "Hari" since it is not in the list and true for "Ram" because it is present. Quickly checks whether an element exists in the list. Returns a simple boolean result.
🌐
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
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); } } Output ·
Published: August 18, 2026
🌐
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!
Top answer
1 of 2
1
  1. You can do it in O(NlogN) time complexity and O(n) memory. Just sort arrays and use two pointers technique.
    List<Integer> answer = new ArrayList<>();
    int j = 0;
    Arrays.sort(nums1);
    Arrays.sort(nums2);
    for(int i = 0; i < nums1.length; i++) {
        if(i > 0 && nums1[i] == nums1[i - 1]) //we have already processed this number
            continue;
        while(j < nums2.length && nums2[j] < nums1[i])
            j++;
        if(j < nums2.length && nums1[i] == nums2[j])
            answer.add(nums1[i]);
    }
    return answer;
  1. You can do it in O(N) time complexity and O(n) memory (but constant is higher). Just add all elements of nums1 in first HashSet and all elements of nums2 if another HashSet. Then you can for each element in first set check if another set contains this element using O(1)* time.
    List<Integer> answer = new ArrayList<>();
    Set<Integer> set1 = new HashSet<>(), set2 = new HashSet<>();
    set1.addAll(nums1);
    set2.addAll(nums2);
    
    for(var el : set1) {
        if(set2.contains(el)) {
            answer.add(el);
        }
    }
    
    return answer;

*O(1) is middle time of operations with hashset

2 of 2
1

If Arrays is a static object already initialized, or declared at global scope, it may be OK, I don't know for sure. Can't call asList() on an uninitialized object, it must be allocated first.

Now I know, Arrays is a member of the utils package, can be OK. But not anything that looks fine in code, actually works also. As a matter of fact, I don't like the way in which Java calls a function. But it would be more easier and handy like this.

I don't know, if you had included the util package in your code. util, or utils ? Can be 2 different packages, this is important. You can try another way:

import java.util.*;

public static List<Integer> arraysIntersection(int[] nums1, int[] nums2){ 
   List<Integer> answer = new ArrayList<>();
   for (int i = 0; i < nums1.length; i++) {
      int u = nums1[i]; 
      for (int j = 0; j < nums2.length; j++) { 
         if (u == nums2[j]) {  
            answer.add(u); 
            break; 
         }
      } 
   }
   return answer;
} 

A break could be necessary, if the values must be added only once. ( a number can be found more times into an array ) The break was meant just for ending the inner loop. The outer loop should continue up to the end of the search.

But the same number can be find more times in the first array. Before returning the answer, the result should be checked for duplicate values. And this is another problem.

It would be more convenient to check before adding number to list. Check if the answer list already contains the number value. And then add the number to the list, if a match is not found. So this can be done more easier:

if (!answer.contains(u)) answer.add(u); 

Since you check this condition, the break is not anymore needed. But searching the list so many times, can slow down your code.

From this reason, the u value is read only once, before starting to compare with the another array. So you don't have to read manytimes the same array item, just store it in a temporary variable, u.

In another point of view, when iterating over a collection object, the current value for the current position index could change. It also depends on the basic interface implementation for this object.

If this function would have been written in C, it would have been a very serious problem, to get the value of an item in array, because the current position of the pointer changes. And here is the same, the current read value changes, excepting that we can't see all the underlying mechanism. And if I am not wrong too much, the original Java first time was based also on the C code.

Best regards, Adrian Brinas. Have a nice day.

🌐
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 - We can use the Arrays class to get the list representation of the array. Then use the contains() method to check if the array contains the value.
🌐
Programiz
programiz.com › java-programming › examples › array-contains-value
Java Program to Check if An Array Contains a Given Value
In the above program, we've used a non-primitive data type String and used Arrays's stream() method to first convert it to a stream and anyMatch() to check if the array contains the given value toFind.
🌐
Facebook
facebook.com › groups › TheEntomologyGroup › posts › 10163232660693393
What is the species of this shiny metallic green soldier fly?
Explore the things you love · Log in to Facebook · English (UK) · Русский · Українська · Suomi · 中文(简体) · Deutsch · العربية · More languages…
🌐
LeetCode
leetcode.com › problems › find-all-duplicates-in-an-array
Find All Duplicates in an Array - LeetCode
Can you solve this real interview question? Find All Duplicates in an Array - Given an integer array nums of length n where all the integers of nums are in the range [1, n] and each integer appears at most twice, return an array of all the integers that appears twice.
🌐
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.
🌐
Baeldung
baeldung.com › home › java › java list › check if a list contains a string element while ignoring case
Check if a List Contains a String Element While Ignoring Case | Baeldung
March 7, 2025 - In this quick tutorial, we’ll explore various methods and strategies to solve this common problem in Java. List provides the convenient contains() method to check if a given value exists in the list.
🌐
Quora
quora.com › How-do-I-check-if-an-array-includes-an-object-in-Java
How to check if an array includes an object in Java - Quora
Answer (1 of 2): Java’s [code ]List[/code] interface specifies a [code ]contains()[/code] method that does what you want, but there’s no equivalent built-in solution for arrays. You’ll have to write your own implementation. But as the saying goes, there’s more than one way to skin a cat.