coderolls Staff
coderolls Staff Hi 👋🏼, At coderolls we write tutorials about Java programming language and related technologies.

ArrayList isEmpty() method in Java

In this tutorial, we will see the isEmpty() method of the ArrayList class in Java. This method is used to check if the ArrayList is empty or not.

Introduction

ArrayList is a widely used class to store and retrieve data in a collection framework.

In previous articles, we have various methods to perform operations on the ArrayList.

A few methods of ArrayList class are add(), addAll(), remove(), set(), iterate(), clear(), contains(), get(), removeRange(), retainAll(), trimToSize(), indexOf() and lastIndexOf(), removeIf() and removeAll() method.

Today we will see the isEmpty() method.

ArrayList removeIf(Predicate filter) method

The isEmpty() method returns true if this list contains no elements.

The isEmpty() method checks if the ArrayList is empty or not. If it does not find any elements in the ArrayList, it will return true, otherwise false.

The method signature is given below

1
public boolean isEmpty()

This method does not accept any parameter.

This method has a return type as a boolean. It returns true if ArrayList is empty.

Let’s see an example Java program for the ArrayList isEmpty() method.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import java.util.ArrayList;

/**
 * An example Java program for an isEmpty method 
 * of the ArrayList class in Java.
 * 
 * @author Gaurav Kukade at coderolls
 */
public class ArrayListIsEmptyExample {

  public static void main(String[] args) {
    //Create an ArrayList teams
    ArrayList<String> teams = new ArrayList<String>();
    
    //Add elements to the ArrayList teams
    teams.add("LA Galaxy");
    teams.add("Orlando City FC");
    teams.add("Portland Timbers");
    teams.add("Columbus Crew");
    
    boolean isTeamsEmpty = teams.isEmpty();
    if(isTeamsEmpty) {
      System.out.println("Teams ArrayList is empty.");
    }
    else {
      System.out.println("Teams ArrayList is not empty.");
    }
    
    // create an ArrayList players
    ArrayList<String> players = new ArrayList<String>();
    
    boolean isPlayersEmpty = players.isEmpty();
    if(isPlayersEmpty) {
      System.out.println("Players ArrayList is empty.");
    }
    else {
      System.out.println("Players ArrayList is not empty.");
    }
  }
}

Output:

1
2
Teams ArrayList is not empty.
Players ArrayList is empty.

Conclusion

The isEmpty() method returns true if this list contains no elements.

1
public boolean isEmpty()

The example Java program used in the above article can be found at this GitHub repository.

Please write your thoughts in the comment section below.

Join Newsletter
Get the latest tutorials right in your inbox. We never spam!

comments powered by Disqus