Languages
[Edit]
EN

Java - check if ArrayList contains value

0 points
Created by:
Sylvie-Justice
490

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.

Alternative titles

  1. Java - check if ArrayList element exist
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.

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