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

🌐
Career Karma
careerkarma.com › blog › java › java array contains
Java Array Contains: The Complete Guide | Career Karma
December 1, 2023 - In Java, arrays are containers that hold data of a single type. For example, an array could hold a list of names of 14 bird species you saw in your backyard bird watching this year. Such an array would contain 14 string values.
Discussions

java - Find if a String is present in an array - Stack Overflow
I tried looking in Java docs but all I am getting is things that I don't want like questions and stuff... so, how is this done? I know there is a "in array" function but I'm not too good with combining the two together. ... String[] dan = {"Red", "Orange", "Yellow", "Green", "Blue", "Violet", "Orange", "Blue"}; boolean contains ... More on stackoverflow.com
🌐 stackoverflow.com
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
java - How can I verify that an array of strings contain a certain string? - Stack Overflow
Given : String[] directions = {"UP","DOWN","RIGHT","LEFT","up","down","right","left"}; String input = "Up"; How can I verify that an input from stdin is within the directions array or not ? I can More on stackoverflow.com
🌐 stackoverflow.com
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
🌐
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 - package com.mkyong.core; import ... PrimitiveArrayExample1 { public static void main(String[] args) { int[] number = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; if(contains(number, 2)){ System.out.println("Hello 2"); } } public static boolean ...
🌐
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 - String[] fruits = new String[] { "banana", "guava", "apple", "cheeku" }; Arrays.asList(fruits).contains("apple"); // true Arrays.asList(fruits).indexOf("apple"); // 2 Arrays.asList(fruits).contains("lion"); // false Arrays.asList(fruits).in...
🌐
Stack Abuse
stackabuse.com › java-check-if-array-contains-value-or-element
Java: Check if Array Contains Value or Element
November 19, 2020 - ... Integer[] intArray = new Integer[]{1, 2, 3, 4, 5}; String[] nameArray = new String[]{"John", "Mark", "Joe", "Bill", "Connor"}; List<Integer> intList = new ArrayList<>(Arrays.asList(intArray)); List<String> nameList = new ArrayList<>(Arrays.asList(nameArray)); System.out.println(intList...
🌐
Java Code Geeks
examples.javacodegeeks.com › home › java development › core java
Java Array Contains Example - Java Code Geeks
August 14, 2020 - In the example, I demonstrated four ways to find an element in an array: Utilize Java AbstractCollection.contains.
🌐
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 - public void searchArrayReuseList() { List asList = Arrays.asList(SearchData.strings); for (int i = 0; i < SearchData.count; i++) { asList.contains("T"); } } public void searchArrayReuseSet() { Set asSet = new HashSet<>(Arrays.asList(SearchD...
Find elsewhere
🌐
Whitman College
whitman.edu › mathematics › java_tutorial › java › nutsandbolts › arraysAndStrings.html
Arrays and Strings
A sequence of character data is called a string and is implemented in the Java environment by the String class (a member of the java.lang package). The character-counting program uses Strings in two different places. The first is in the definition of the main() method: String[] args This code explicitly declares an array, named args, that contains String objects. The empty brackets indicate that the length of the array is unknown at compilation time because the array is passed in at runtime. The second use of Strings in the example program are these two uses of literal strings (a string of characters between double quotation marks " and "):
🌐
TutorialKart
tutorialkart.com › java › java-array › java-array-contains
How to check if Java Array Contains specific Object/Element?
November 23, 2020 - In this tutorial, we will learn how to check if a Java Array contains a given value or not. To check if an element is present in the array or not, we have to traverse through the array elements one by one, and check if the element of array equals the search element. The logic to check the equality may change with datatype. For example, to check if two integers are equal, we can use equal to comparison operator, but to check if two strings are equal, we may have to use String.equals() function.
🌐
Delft Stack
delftstack.com › home › howto › java › array contains java
How to Check an Array Contains a Particular Value in Java | Delft Stack
February 2, 2024 - package findInList; import ... main(String[] args) { String[] arr = new String[] {"1", "2", "3"}; String stringToSearch = "2"; System.out.println(Arrays.asList(arr).contains(stringToSearch)); } }...
🌐
Programiz
programiz.com › java-programming › examples › array-contains-value
Java Program to Check if An Array Contains a Given Value
import java.util.Arrays; class Main { public static void main(String[] args){ String[] strings = {"One","Two","Three","Four","Five"}; String toFind= "Four"; boolean found = Arrays.stream(strings).anyMatch(t -> t.equals(toFind)); if(found) System.out.println(toFind + " is found."); else ...
🌐
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 - For example, to find a string in an array of strings, we could use the following code: import java.util.Arrays; public class Main { public static void main(String[] args) { String[] products = new String[]{"Coffee", "Tea", "Chocolate Bar"}; if ...
🌐
Centron
centron.de › home › tutorials › how to check if java array contains a value?
How to Check if Java Array Contains a Value? – centron
December 1, 2024 - Let’s use JShell to run the example code snippet. ... jshell> String[] vowels = { "A", "I", "E", "O", "U" }; vowels ==> String[5] { "A", "I", "E", "O", "U" } jshell> List vowelsList = Arrays.asList(vowels); vowelsList ==> [A, I, E, O, U] jshell> vowelsList.contains("U") $3 ==> true jshell> vowelsList.contains("X") $4 ==> false · If you are using Java 8 or higher, you can create a stream from the array.
🌐
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!

🌐
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 ...
🌐
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!
🌐
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"}; // convert array to list List<String> list = Arrays.asList(names); // check if `Emma` exists in list if(list.contains("Emma")) { System.out.println("Hi, Emma ?"); } ... In Java 8 or higher, you ...
🌐
Apps Developer Blog
appsdeveloperblog.com › home › java › java examples › check if array contains a value in java
Check if Array Contains a Value in Java - Apps Developer Blog
August 16, 2022 - In this way, we are implementing our logic to check if array contains a particular value, which is, in our case, the word “Java”. ... class CheckIfArrayContainsValue { public static void main(String[] args) { String[] programmingLanguages = {"Python", "Kotlin", "Ruby", "JavaScript", "C#", "Java", "Flutter"}; for (String lang : programmingLanguages) { if (lang.equals("Java")) { System.out.println("It does contain!"); break; // value found, exit the loop } } } }