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(S...
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
java - Find if a String is present in an array - Stack Overflow
OK let's say I have an array filled ... and if NOT to get like a message saying "Command not found". 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
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
Java - search a string in string array - Stack Overflow
I think it's important to note ... with checking each element in the array to see if it matches x, it just hides that work from the programmer. Not a knock on this answer. People should be aware, that's all. 2016-07-12T16:29:53.05Z+00:00 ... And consider this creates an ArrayList object just to search for a string. 2018-09-09T13:58:10.177Z+00:00 ... This solution is raising Critical code smell in SonarQube as "This call to 'contains()' may be ... More on stackoverflow.com
🌐 stackoverflow.com
🌐
Stack Abuse
stackabuse.com › java-check-if-array-contains-value-or-element
Java: Check if Array Contains Value or Element
November 19, 2020 - 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: Integer[] arr = new Integer[]{1, 2, 3, 4, 5}; System.out.println(Arrays.stream(arr).anyMatch(x -> x == 3)); ... String[] arr = new String[]{"John", "Mark", "Joe", "Bill", "Connor"}; String searchString = "Michael"; boolean doesContain = Arrays.stream(arr) .anyMatch(x -> x.equals(searchString)); System.out.println(doesContain);
🌐
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.
🌐
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!

🌐
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 - 1. Using Arrays Class 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 …
🌐
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!
Find elsewhere
🌐
Java67
java67.com › 2014 › 11 › how-to-test-if-array-contains-certain-value-in-java.html
How to check if Array contains given Number or String in Java [ Linear Search vs Binary Search] | Java67
Without wasting any more of your time, here is our complete Java program to search any given String or integer value in the given array. If you are using Eclipse IDE, just copy-paste the code and run it, you don't need to create a Java source file, Eclipse will take care of that, provided you have selected a Java project. import java.util.Arrays; /** * Java Program to check if an array contains a value or not.
🌐
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 java.util.Arrays; import java.util.List; public class 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 contains(final int[] array, final int v) { boolean result = false; for(int i : array){ if(i == v){ result = true; break; } } return result; } }
🌐
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.
🌐
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)); ...
🌐
Programiz
programiz.com › java-programming › examples › array-contains-value
Java Program to Check if An Array Contains a Given Value
If any of the element n returns true, found is set to true as well. 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 System.out.println(toFind + " is not found."); } }
🌐
TutorialKart
tutorialkart.com › java › java-array › java-array-contains
How to check if Java Array Contains specific Object/Element?
November 23, 2020 - 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.
🌐
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 can also use the Stream API to check if a value exists in an array as shown below:
🌐
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
Return Type: It return a boolean value, if the element is present in the list return true, otherwise return false. Java · 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
Top answer
1 of 16
270

EDIT: Here is an update using the Java 8 Streaming API. So much cleaner. Can still be combined with regular expressions too.

public static boolean stringContainsItemFromList(String inputStr, String[] items) {
    return Arrays.stream(items).anyMatch(inputStr::contains);
}

Also, if we change the input type to a List instead of an array we can use items.stream().anyMatch(inputStr::contains).

You can also use .filter(inputStr::contains).findAny() if you wish to return the matching string.

Important: the above code can be done using parallelStream() but most of the time this will actually hinder performance. See this question for more details on parallel streaming.


Original slightly dated answer:

Here is a (VERY BASIC) static method. Note that it is case sensitive on the comparison strings. A primitive way to make it case insensitive would be to call toLowerCase() or toUpperCase() on both the input and test strings.

If you need to do anything more complicated than this, I would recommend looking at the Pattern and Matcher classes and learning how to do some regular expressions. Once you understand those, you can use those classes or the String.matches() helper method.

public static boolean stringContainsItemFromList(String inputStr, String[] items)
{
    for(int i =0; i < items.length; i++)
    {
        if(inputStr.contains(items[i]))
        {
            return true;
        }
    }
    return false;
}
2 of 16
61
import org.apache.commons.lang.StringUtils;

String Utils

Use:

StringUtils.indexOfAny(inputString, new String[]{item1, item2, item3})

It will return the index of the string found or -1 if none is found.