Report this

What is the reason for this report?

How to Check if Java Array Contains a Value?

Published on August 3, 2022
How to Check if Java Array Contains a Value?

How to Check if Java Array Contains a Value?

There are many ways to check if a Java array contains a specific value.

  • Simple iteration using for loop
  • List contains() method
  • Stream anyMatch() method
  • Arrays binarySearch() for sorted array

Let’s look into all these methods one at a time.

1. Using For Loop

This is the easiest and convenient method to check if the array contains a certain value or not. We will go over the array elements using the for loop and use the equals() method to check if the array element is equal to the given value.

String[] vowels = { "A", "I", "E", "O", "U" };

// using simple iteration over the array elements
for (String s : vowels) {
	if ("E".equals(s)) {
		System.out.println("E found in the vowels list.");
	}
}

2. Using List contains() method

We can use Arrays class to get the list representation of the array. Then use the contains() method to check if the array contains the value. 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

Java Array Contains Value

3. Using Stream anyMatch() Method

If you are using Java 8 or higher, you can create a stream from the array. Then use the anyMatch() method with a lambda expression to check if it contains a given value.

jshell> List vowelsList = Arrays.asList(vowels);
vowelsList ==> [A, I, E, O, U]

jshell> Arrays.stream(vowels).anyMatch("O"::equals);
$5 ==> true

jshell> Arrays.stream(vowels).anyMatch("X"::equals);
$6 ==> false

4. Arrays binarySearch() for sorted array

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

Arrays.parallelSort(vowels);

System.out.println("Sorted Array = " + Arrays.toString(vowels));

int index = Arrays.binarySearch(vowels, "X");

if (index < 0) {
	System.out.println("X not found in the array");
} else {
	System.out.println("X found in the array");
}

Output:

Unsorted Array = [A, I, E, O, U]
Sorted Array = [A, E, I, O, U]
X not found in the array

Checking if Array Contains Multiple Values

What if we want to check if the array contains multiple values. Let’s say you want to check if a given array is the subset of the source array. We can create nested loops and check each element one by one. There is a cleaner way by converting arrays to list and then use the containsAll() method.

String[] vowels = { "A", "I", "E", "O", "U" };
String[] subset = { "E", "U" };

boolean foundAll = Arrays.asList(vowels).containsAll(Arrays.asList(subset));

System.out.println("vowels contains all the elements in subset = " + foundAll);

Output: vowels contains all the elements in subset = true

References

  1. Arrays binarySearch() API Doc
  2. Stream anyMatch() API Doc

Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

Learn more about our products

About the author

Pankaj Kumar
Pankaj Kumar
Author
See author profile

Java and Python Developer for 20+ years, Open Source Enthusiast, Founder of https://www.askpython.com/, https://www.linuxfordevices.com/, and JournalDev.com (acquired by DigitalOcean). Passionate about writing technical articles and sharing knowledge with others. Love Java, Python, Unix and related technologies. Follow my X @PankajWebDev

Category:
Tags:
While we believe that this content benefits our community, we have not yet thoroughly reviewed it. If you have any suggestions for improvements, please let us know by clicking the “report an issue“ button at the bottom of the tutorial.

Still looking for an answer?

Was this helpful?

What is the array is of integers?

- abc xyz

Creative CommonsThis work is licensed under a Creative Commons Attribution-NonCommercial- ShareAlike 4.0 International License.
Join the Tech Talk
Success! Thank you! Please check your email for further details.

Please complete your information!

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.