Languages
[Edit]
EN

Java - check if ArrayList contains value

0 points
Created by:
Sylvie-Justice
250

In this article, we would like to show you how to check if ArrayList contains a value in Java.

Quick solution:

myArrayList.contains(value);

 

Practical example

In this example, we check if letters ArrayList contains an element with C value using contains() method that returns true or false.

import java.util.*;

public class Example {

    public static void main(String[] args) {
        List<String> letters = new ArrayList<>();

        letters.add("A");
        letters.add("B");
        letters.add("C");

        if (letters.contains("C")) {
            System.out.println("The ArrayList contains C.");
        } else {
            System.out.println("The ArrayList doesn't contain C.");
        }
    }
}

Output:

The ArrayList contains C.

Java Collections - ArrayList

Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join