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
464

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

🌐
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(). Syntax: public boolean contains(Object element) Parameter: It takes a single parameter Object ...
Published   November 10, 2025
Discussions

[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
[Java] Implementing an ArrayList and the contains method and indexOf methods are returning wrong values.
First: your contains method has a major bug: you call i++ twice per iteration, if the value is not the one you're searching for. So you skip every odd-numbered index on your array. That's probably the source of your errors. You need to completely remove the else condition. Second: It's smart that you used one method in the other, but you did it backwards. contains should call indexOf, not the other way around, just for basic efficiency purposes. Think of it this way: with a corrected version of your contains, you have to fully traverse the array to determine that the value doesn't exist. But let's assume that the value we're searching for exists, but is in the last cell of the array. Now, you have to traverse the whole array once, to see if it exists, then again, to find the index it happens at. But if you flip this logic on its head, now you just traverse once to find both pieces of information. Either it exists, and you get an index, or it doesn't, and you get a -1. Your contains could be as simple as return indexOf(o) >= 0;. indexOf should be where the actual work happens, and contains just interprets the result of it as true or false. More on reddit.com
🌐 r/learnprogramming
9
1
January 16, 2019
Parameter 0 of function 'array_contains()' requires an array type, but argument is of type 'java.lang.String[]'
Tested with latest 6.6.0-SNAPSHOT: org.hibernate.query.sqm.produce.function.FunctionArgumentException: Parameter 0 of function 'array_contains()' requires an array type, but argument is of type 'java.lang.String[]' at org.hibernate.dialect.function.array.ArrayArgumentValidator.getPluralTyp... More on discourse.hibernate.org
🌐 discourse.hibernate.org
0
May 14, 2024
How do you check if a java array contains a certain element?
. contains does work.. myStringArray[index].contains("h"); or even shorter if you don't want to iterate through manually Arrays.asList(yourArray).contains(yourValue) More on reddit.com
🌐 r/javahelp
3
3
April 10, 2020
🌐
DigitalOcean
digitalocean.com › community › tutorials › java-array-contains-value
How to Check if a Java Array Contains a Value? | DigitalOcean
September 16, 2025 - Note: Generics in Java do not work with primitive types (int, double, etc.). To use this method with primitives, you must use their corresponding wrapper classes (Integer, Double, etc.). While it’s good to know how to implement these checks yourself, you don’t always have to reinvent the wheel. The Java ecosystem is rich with libraries that provide robust, well-tested utility functions. One of the most popular is Apache Commons Lang. Its ArrayUtils class offers a convenient contains() method that simplifies the entire process.
🌐
W3Schools
w3schools.com › java › ref_arraylist_contains.asp
Java ArrayList contains() Method
Java OOP Java Classes/Objects Java Class Attributes Java Class Methods Java Class Challenge Java Constructors Java this Keyword Java Modifiers · Access Modifiers Non-Access Modifiers Java Encapsulation Java Packages / API Java Inheritance Java Polymorphism Java super Keyword Java Inner Classes Java Abstraction Java Interface Java Anonymous Java Enum ... Java Data Structures Java Collections Java List Java ArrayList Java LinkedList Java List Sorting Java Set Java HashSet Java TreeSet Java LinkedHashSet Java Map Java HashMap Java TreeMap Java LinkedHashMap Java Iterator Java Algorithms
🌐
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 - Let’s start with three methods that implement each algorithm: boolean searchList(String[] strings, String searchString) { return Arrays.asList(SearchData.strings) .contains(searchString); } boolean searchSet(String[] strings, String searchString) { Set<String> stringSet = new HashSet<>(Arrays.asList(SearchData.strings)); return stringSet.contains(searchString); } boolean searchLoop(String[] strings, String searchString) { for (String string : SearchData.strings) { if (string.equals(searchString)) return true; } return false; }
🌐
Educative
educative.io › answers › what-is-the-arraylistcontains-method-in-java
What is the ArrayList.contains() method in Java?
The ArrayList.contains() method in Java is used to check whether or not a list contains a specific element.
Find elsewhere
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › util › Arrays.html
Arrays (Java Platform SE 8 )
October 20, 2025 - Searches the specified array of longs for the specified value using the binary search algorithm. The array must be sorted (as by the sort(long[]) 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, ...
🌐
Career Karma
careerkarma.com › blog › java › java array contains
Java Array Contains: The Complete Guide | Career Karma
December 1, 2023 - You can also check whether an array contains a particular value using the Java contains() method.
🌐
Coderanch
coderanch.com › t › 406173 › java › determine-string-array-string
How can I determine if a string array contains a particular string? (Beginning Java forum at Coderanch)
February 15, 2007 - And the collection classes that implement List all have a method "contains". You can take an ArrayList for example: ... If you don't like generics in your compositions, you can omit the <String> in this case. Yours, Bu. ... In an unordered list (java.util.List, array), you will definitely have ...
🌐
Codecademy
codecademy.com › docs › java › arraylist › .contains()
Java | ArrayList | .contains() | Codecademy
February 22, 2024 - It is used to check if the element is present in the specified ArrayList or not. The function returns a boolean value of true if the element is present and false if not. ... Looking for an introduction to the theory behind programming?
🌐
Reddit
reddit.com › r/learnprogramming › [java] implementing an arraylist and the contains method and indexof methods are returning wrong values.
r/learnprogramming on Reddit: [Java] Implementing an ArrayList and the contains method and indexOf methods are returning wrong values.
January 16, 2019 -

So I am working on implementing an arrayList for a class of mine and I'm having problems getting the indexOf and contains methods working. For example I run this code to test

    System.out.println("Location of \"c\" (should be 2): " + myList.indexOf("c"));
    System.out.println("Location of \"z\" (should be -1): " + myList.indexOf("z"));
    System.out.println("Contains \"a\" (should be true): " + myList.contains("a"));  

but when I run it this is what is printed out:

Location of "c" (should be 2): -1
Location of "z" (should be -1): -1
Contains "a" (should be true): false  

Which means something must be wrong with my indexOf method and my contains method. My indexOf methods use contains so I think my contains is the source of the problem

For my contains method the code looks like this

public boolean contains(T o) {
    for (int i = 0;i < size; i++){ 
		if (array[i].equals(o))
			return true;
		else
			i++;
    }
    return false;
}

I'm trying to find if the list contains the object but it consistently returns false.

While I think it's the contains method that's screwing up I'm not certain if my indexOf method might be screwing it up too and the code for that one is here:

public int indexOf(T o) {
      if(contains(o).equals(false))
        return -1; 
    		for (int i = 0; i < size ; i++) {
		if (get(i).equals(o)) {
			return i;
		}
	}
	return -1;
}

I can't figure out why the values I want aren't coming through correctly and any advice would be appreciated

🌐
Stack Abuse
stackabuse.com › java-check-if-array-contains-value-or-element
Java: Check if Array Contains Value or Element
November 19, 2020 - There are various ways to convert a Java array to an ArrayList, though, we'll be using the most widely used approach. Then, we can use the contains() method on the resulting ArrayList, which returns a boolean signifying if the list contains the element we've passed to it or not.
🌐
Programiz
programiz.com › java-programming › examples › array-contains-value
Java Program to Check if An Array Contains a Given Value
Become a certified Java programmer. Try Programiz PRO! ... class Main { public static void main(String[] args) { int[] num = {1, 2, 3, 4, 5}; int toFind = 3; boolean found = false; for (int n : num) { if (n == toFind) { found = true; break; } } if(found) System.out.println(toFind + " is found."); else System.out.println(toFind + " is not found."); } } ... In the above program, we have an array of integers stored in variable num.
🌐
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 - 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.
🌐
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 - This method is part of the Arrays utility class and offers optimized performance for large, sorted arrays. Remember to sort the array before performing a binary search to ensure correct results. ... import java.util.Arrays; public class Main { public static void main(String[] args) { int[] array = {1, 2, 3, 4, 5}; int valueToFind = 3; Arrays.sort(array); int result = Arrays.binarySearch(array, valueToFind); boolean found = result >= 0; System.out.println("Value found: " + found); } } Explain Code
🌐
Quora
quora.com › How-do-you-implement-a-contains-method-in-ArrayList-Java-arraylist-contains-development
How to implement a contains method in ArrayList (Java, arraylist, contains, development) - Quora
Answer: Why would you do that when Arraylist already has an implementation of contains? In any case you can look at the source code in the JDK and see the implementation. It is quite simple. If the passed in object is not null It simply loops through the backing array and compares each element t...
🌐
Crunchify
crunchify.com › java j2ee tutorials › 4 ways to check if an array contains a specific value – intstream, arrays.aslist (linear search algorithm)
4 Ways to Check if an Array Contains a Specific Value - IntStream, Arrays.asList (Linear Search Algorithm) • Crunchify
November 17, 2022 - Well, then follow detailed tutorial on how to override contains() / findMe() method by yourself. Let’s understand first our logic and what we are going to do in this Java tutorial. ... Test-3: Create crunchifyLongArray with 15 elements and perform check with Java8 Utility using LongStream -> anyMatch() Test-4: Perform same contains check using Arrays.asList() method
🌐
GeeksforGeeks
geeksforgeeks.org › java › arraylist-contains-java
Arraylist.contains() Method in Java - GeeksforGeeks
December 10, 2024 - In Java, the ArrayList contains() method is used to check if the specified element exists in an ArrayList or not.
🌐
Hibernate
discourse.hibernate.org › hibernate orm
Parameter 0 of function 'array_contains()' requires an array ...
May 14, 2024 - Tested with latest 6.6.0-SNAPSHOT: org.hibernate.query.sqm.produce.function.FunctionArgumentException: Parameter 0 of function 'array_contains()' requires an array type, but argument is of type 'java.lang.String[]' at org.hibernate.dialect.function.array.ArrayArgumentValidator.getPluralTyp...