How to Check if an Arraylist is Empty in Java

Sometimes as a Java programmer, you need to know that if an arraylist is empty or not. We present here some of the ways used to do the job.

Different programmers might use different methods to determine whether an array is empty. But first let’s see what an empty arraylist is.

Empty arraylists

An arraylist with no element is called empty. This usually happens when an arraylist has just been created and there is nothing in it.

This could happen on other occasions as well. For example, when you clear a list with any number of element, you’ll get an empty list as a result.

So now that we’re sure about the meaning of the term we’re using here, let’s talk about the methods we’re going to use to check for an empty array.

There are two commonly-used methods which are able to do the task for us: size() and isEmpty().

size() method

This method is used when you have an arraylist or set and want to determine the number of elements in them.

Obviously, for our goal, if the result is 0 then we have an empty arraylist. Otherwise, it’s not empty.

Let’s take a look at this example:

public class CheckArrayList

{

public static void main(String[] args)

{

ArrayList<Integer> alist = new ArrayList<>();

System.out.println(alist.size());

}

}

Here we have an arraylist of integer values. When we check the size of the list, the method returns 0 since there is no element in the list yet.

So, we could know that the list is empty.

But, what if we add an element to the list with the following line:

alist.add(1);

This way, if we check the size of the arraylist again, we’ll see that the method no longer returns 0.

In fact, here we have an arraylist with one element.

isEmpty() method

Another way to check for an empty arraylist is using the isEmpty() method.

The process is very similar to the size() method we mentioned earlier. The only difference is that here we have a method which returns Boolean values: true and false.

As you can guess, in case we have an empty arraylist the method returns true. Otherwise the result would be false.

Now that we’ve learned the process in theory, let’s practice it with an example:

public class CheckArrayList2

{

public static void main(String[] args)

{

ArrayList<Integer> alist = new ArrayList<>();

System.out.println(alist.isEmpty());

}

}

Here we have an arraylist of integer values again. This time, to check the emptiness of the arraylist we used the isEmpty method.

As you could see, since the method is used just after creating the list, it would return true. It means that the list is empty because there is nothing in the arraylist.

But let’s imagine that we have this line after creating the arraylist:

alist.add(1);

As you probably remember form the previous example, this changes the result.

By adding an integer value to the list, say number 1 for instance, we now have an arraylist with one element.

So, if we try the isEmpty() method again after having this line in our code we no longer get a true in our result.