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

🌐
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 - 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; }
Discussions

How do I know if an array list contains an specific string
Please ensure that: Your code is properly formatted as code block - see the sidebar (About on mobile) for instructions You include any and all error messages in full You ask clear questions You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions. Trying to solve problems on your own is a very important skill. Also, see Learn to help yourself in the sidebar If any of the above points is not met, your post can and will be removed without further warning. Code is to be formatted as code block (old reddit: empty line before the code, each code line indented by 4 spaces, new reddit: https://imgur.com/a/fgoFFis ) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc. Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit. Code blocks look like this: public class HelloWorld { public static void main(String[] args) { System.out.println("Hello World!"); } } You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above. If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures. To potential helpers Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice. I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns. More on reddit.com
🌐 r/javahelp
14
9
May 12, 2022
how to check if two items are in a java array
Please, do not post in multiple subreddits and if you do so, cross-link the posts. This avoids unnecessary redundancy. More on reddit.com
🌐 r/learnjava
9
2
September 22, 2022
[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 why i cant check if string contains char?
i can do if(test[x].contains(“char”); “char” is a String, not a char. Contains presumably expects a string. If you try to call it with a char, it will fail. Edit: actually it expects a CharSequence. String implements CharSequence, but char does not. More on reddit.com
🌐 r/learnprogramming
3
2
March 30, 2019
🌐
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 to check every element to ensure that the element you are looking for isn't the last element in the list.
🌐
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!
🌐
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 ...
🌐
Career Karma
careerkarma.com › blog › java › java array contains
Java Array Contains: The Complete Guide | Career Karma
December 1, 2023 - The contains() method can only be used to check whether an array contains a string value. If you want to search for a number in an array, you may want to use the for-each loop technique instead.
Find elsewhere
🌐
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)); ...
🌐
Reddit
reddit.com › r/javahelp › how do i know if an array list contains an specific string
r/javahelp on Reddit: How do I know if an array list contains an specific string
May 12, 2022 -

I created an array list out from a constructor, now what I want to do is that the program will only print those set of array list with the item that matches the search key. I tried using arraylist.contains but it won't work. Is there any method I can use to know whether the array contains a specific certain string? Thank you!

🌐
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.
🌐
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
🌐
Mkyong
mkyong.com › home › java › java – check if array contains a certain value?
Java – Check if Array contains a certain value? | mkyong.com
November 19, 2016 - 1.2 Example to check if a String Array contains multiple values : ... package com.mkyong.core; import java.util.Arrays; import java.util.List; public class StringArrayExample2 { public static void main(String[] args) { String[] alphabet = new String[]{"A", "C"}; // Convert String Array to List List<String> list = Arrays.asList(alphabet); // A or B if (list.contains("A") || list.contains("B")) { System.out.println("Hello A or B"); } // A and B if (list.containsAll(Arrays.asList("A", "B"))) { System.out.println("Hello A and B"); } // A and C if (list.containsAll(Arrays.asList("A", "C"))) { System.out.println("Hello A and C"); } } }
🌐
GeeksforGeeks
geeksforgeeks.org › java › string-arrays-in-java
String Arrays in Java - GeeksforGeeks
October 2, 2025 - A String Array in Java is an array that stores string values.
🌐
Quora
quora.com › Can-you-make-an-array-that-contains-an-int-and-string-in-Java
Can you make an array that contains an int and string in Java? - Quora
Answer (1 of 6): Sort of. If you’re referring to the int primitive, no, you cannot. Arrays have to have a single type for all the elements they contain. However, if you’re referring to the Integer object, you can, by declaring an Array to contain Objects, i.e.: [code]Object[] foo = new ...
🌐
Sentry
sentry.io › sentry answers › java › find a specific value in an array in java
Java Find a Specific Value in an Array Using Streams | Sentry
September 23, 2026 - Use the Stream API's anyMatch method to check if a Java array contains a value, or convert the array to a list with Arrays.asList for reference types
🌐
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
December 20, 2024 - public class Main { public static void main(String[] args) { int[] array = {1, 2, 3, 4, 5}; int valueToFind = 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 for loop iterates directly over each element, eliminating the need for index manipulation. Utilize Java Stream API introduced in Java 8 to check for the presence of a value in a more functional style.
🌐
Reddit
reddit.com › r/learnjava › how to check if two items are in a java array
r/learnjava on Reddit: how to check if two items are in a java array
September 22, 2022 -

let say i have an array "java_joined_ids [13, 11] " and another array "java_detailId['10', '11']" and another variable "java_content_type_id" which has a value of 11.i need to check the following on page load

need to check if java_detailid1(10),java_content_type_id(11) is in java_joined_ids [] .

similarly java_detailid2(11),java_content_type_id(11) is in java_joined_ids []

the structure of java_joined_ids [java_detailId,java_content_type_id ] .

🌐
Stack Abuse
stackabuse.com › java-check-if-array-contains-value-or-element
Java: Check if Array Contains Value or Element
November 19, 2020 - We've gone over converting the array to a list and calling the contains() method, using a for-loop, the Java 8 Stream API, as well as Apache Commons.
🌐
W3Schools
w3schools.com › java › java_arrays.asp
Java Arrays
Java Examples Java Videos Java Compiler Java Exercises Java Quiz Java Code Challenges Java Practice Problems Java Server Java Syllabus Java Study Plan Java Interview Q&A ... Arrays are used to store multiple values in a single variable, instead ...
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › util › Arrays.html
Arrays (Java Platform SE 8 )
July 21, 2026 - If it is not sorted, the results are undefined. (If the array contains elements that are not mutually comparable (for example, strings and integers), it cannot be sorted according to the natural ordering of its elements, hence results are undefined.) If the array contains multiple elements equal to the specified object, there is no guarantee which one will be found.
🌐
Linux Hint
linuxhint.com › checking-java-array-contains-value
Checking If a Java Array Contains a Value – Linux Hint
Create a Java file with the following code that takes an input from the user and check whether the input value exists in the array or not using the contains() method. This method returns true if the input value exists in the array. Otherwise, this method returns false.