Tutorials  /  JavaScript

How to Check if Java Array Contains a Value?

Ccentron Redaktion · December 2024 ·3 min read ·JavaScript, Tutorial

There are many ways to check if Java Array Contains a 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.

Using For Loop to check if Java Array Contains a Value

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.

Code

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.");
    }
}
VM

Matching infrastructure at centron

Node applications need somewhere to run: ccloud³ VMs with full root access from €3.12 per month – or as a managed server if you would rather not run it yourself. Rent a cloud server →

Using List to check if Java Array Contains a Value contains() Method

We can use the 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.

Code

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

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.

Code

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

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.

Code

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:

Code

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 lists and then using the containsAll() method.

Code

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:

Code

vowels contains all the elements in subset = true
Jetzt 200 € Guthaben sichern

Testen Sie Ihr Setup auf ccloud³

Registrieren Sie sich in der ccloud³ und erhalten Sie 200 € Startguthaben für Ihr Projekt – z. B. für eine PostgreSQL-VM mit automatischen Backups.

centron Redaktion Technische Redaktion

Das Redaktionsteam von centron schreibt Anleitungen aus dem Betriebsalltag: getestet auf unserer eigenen Plattform, betrieben im Rechenzentrum in Hallstadt bei Bamberg.

Kategorie JavaScript
Teilen
Noch offene Fragen?

Unser Team hilft Ihnen bei Ihrem konkreten Setup weiter – von Menschen, die die Plattform selbst betreiben.

War dieses Tutorial hilfreich?

Ihre Antwort wird anonym gespeichert und hilft uns, die Tutorials zu verbessern.

Kommentare

Noch keine Kommentare – stellen Sie die erste Frage zu diesem Tutorial.

Zum Kommentieren anmelden

Kommentare stehen centron-Kunden offen. Melden Sie sich in Ihrem Konto an, um eine Frage zu diesem Tutorial zu stellen.

Weiterlesen

Das könnte Sie auch interessieren

Jetzt kostenlos anfangen

Melden Sie sich an und erhalten Sie in den ersten 60 Tagen ein Guthaben von 200 € bei centron.

Dieses Werbeangebot gilt nur für neue Konten. Angebot ausschließlich für Gewerbetreibende.

Jetzt loslegen Sales kontaktieren