Check if ArrayList is empty example in Java shows how to check if ArrayList is empty or not using the size or isEmpty method.
How to check if ArrayList is empty in Java?
There are a couple of ways to check if the ArrayList is empty as given below.
1) Using the size method
You can use the size
method of the ArrayList class to check if the ArrayList is empty. If the ArrayList size is equal to 0 then the ArrayList is empty, otherwise not.
1 |
public int size() |
This method returns the number of elements contained in the ArrayList.
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 |
package com.javacodeexamples.collections.arraylist; import java.util.ArrayList; public class ArrayListCheckIfEmptyExample { public static void main(String[] args) { ArrayList<String> aListLanguage = new ArrayList<String>(); System.out.println( "Is ArrayList empty? " + isArrayListEmpty(aListLanguage) ); //add some elements to ArrayList aListLanguage.add("C++"); aListLanguage.add("Java"); System.out.println( "Is ArrayList empty? " + isArrayListEmpty(aListLanguage) ); } private static boolean isArrayListEmpty(ArrayList list){ /* * Use size method of ArrayList to * check if ArrayList is empty */ if( list.size() == 0 ) return true; return false; } } |
Output
1 2 |
Is ArrayList empty? true Is ArrayList empty? false |
2) Using the isEmpty method
The ArrayList class provides the isEmpty
method just for this specific purpose.
1 |
public boolean isEmpty() |
This method returns true if the ArrayList does not contain any elements, false otherwise.
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 |
package com.javacodeexamples.collections.arraylist; import java.util.ArrayList; public class ArrayListCheckIfEmptyExample { public static void main(String[] args) { ArrayList<String> aListLanguage = new ArrayList<String>(); System.out.println( "Is ArrayList empty? " + isArrayListEmpty(aListLanguage) ); //add some elements to ArrayList aListLanguage.add("C++"); aListLanguage.add("Java"); System.out.println( "Is ArrayList empty? " + isArrayListEmpty(aListLanguage) ); } private static boolean isArrayListEmpty(ArrayList list){ /* * Use isEmpty method of ArrayList to * check if ArrayList is empty */ return list.isEmpty(); } } |
Output
1 2 |
Is ArrayList empty? true Is ArrayList empty? false |
What is the preferred method to check?
Let’s look at the source code of both of these methods.
size method source code
1 2 3 |
public int size() { return size; } |
isEmpty method source code
1 2 3 |
public boolean isEmpty() { return size == 0; } |
Both of the methods rely on the size member variable. So in terms of the performance both the methods, i.e. size
and isEmpty
, are moreover the same. Using the isEmpty
is recommended method because it clearly tells the purpose of the statement and it produces cleaner and easier to understand code too.
This example is a part of the ArrayList in Java tutorial.
Please let me know your views in the comments section below.