Java ArrayList isEmpty() Method



Description

The Java ArrayList isEmpty() method returns true if this list contains no elements. This method always returns the result as per the current state of the list. If an element is added then isEmpty() will return false.

Declaration

Following is the declaration for java.util.ArrayList.isEmpty() method

public boolean isEmpty()

Parameters

NA

Return Value

This method returns true if this list contains no elements, else false.

Exception

NA

Checking if an ArrayList of Integers is Empty or Not Example

The following example shows the usage of Java ArrayList isEmpty() method. Here we are working with an ArrayList of Integers. At first, we initialize an ArrayList object and then check if it is empty or not. Then we'll be adding few elements and then check again if ArrayList object is empty or not.

package com.tutorialspoint;

import java.util.ArrayList;

public class ArrayListDemo {
   public static void main(String[] args) {
      
      // create an empty array list 
      ArrayList<Integer> arrayList = new ArrayList<>();

      if (arrayList.isEmpty()) {
         System.out.println("arrayList is empty");
      } else {
         System.out.println("arrayList is not empty");
      }

      // printing all the elements available in arrayList
      System.out.println("ArrayList = " + arrayList);
	  
      // use add() method to add elements in the arrayList
      arrayList.add(0);
      arrayList.add(1);
      arrayList.add(2);
      arrayList.add(3);
      arrayList.add(4);
      arrayList.add(5);
      arrayList.add(6);
	
      if (arrayList.isEmpty()) {
         System.out.println("arrayList is empty");
      } else {
         System.out.println("arrayList is not empty");
      }

      // printing all the elements available in arrayList
      System.out.println("ArrayList = " + arrayList);
   }
}

Output

Let us compile and run the above program, this will produce the following result −

arrayList is empty
ArrayList = []
arrayList is not empty
ArrayList = [0, 1, 2, 3, 4, 5, 6]

Checking if an ArrayList of Strings is Empty or Not Example

The following example shows the usage of Java ArrayList isEmpty() method. Here we are working with an ArrayList of Strings. At first, we initialize an ArrayList object and then check if it is empty or not. Then we'll be adding few elements and then check again if ArrayList object is empty or not.

package com.tutorialspoint;

import java.util.ArrayList;

public class ArrayListDemo {
   public static void main(String[] args) {
      
      // create an empty array list
      ArrayList<String> arrayList = new ArrayList<>();

      if (arrayList.isEmpty()) {
         System.out.println("arrayList is empty");
      } else {
         System.out.println("arrayList is not empty");
      }

      // printing all the elements available in arrayList
      System.out.println("ArrayList = " + arrayList);

      // use add() method to add elements in the arrayList
      arrayList.add("A");
      arrayList.add("B");
      arrayList.add("C");
	  
      if (arrayList.isEmpty()) {
         System.out.println("arrayList is empty");
      } else {
         System.out.println("arrayList is not empty");
      }

      // printing all the elements available in arrayList
      System.out.println("ArrayList = " + arrayList);     
   }
}

Output

Let us compile and run the above program, this will produce the following result −

arrayList is empty
ArrayList = []
arrayList is not empty
ArrayList = [A, B, C]

Checking if an ArrayList of Objects is Empty or Not Example

The following example shows the usage of Java ArrayList isEmpty() method. Here we are working with an ArrayList of Student objects. At first, we initialize an ArrayList object and then check if it is empty or not. Then we'll be adding few elements and then check again if ArrayList object is empty or not.

package com.tutorialspoint;

import java.util.ArrayList;

public class ArrayListDemo {
   public static void main(String[] args) {

      // create an empty arrayList
      ArrayList<Student> arrayList = new ArrayList<>();
      
      if (arrayList.isEmpty()) {
         System.out.println("arrayList is empty");
      } else {
         System.out.println("arrayList is not empty");
      }

      // printing all the elements available in arrayList
      System.out.println("ArrayList = " + arrayList);
      // use add() method to add elements in the arrayList
      arrayList.add(new Student(1, "Julie"));
      arrayList.add(new Student(2, "Robert"));
      arrayList.add(new Student(3, "Adam"));

      if (arrayList.isEmpty()) {
         System.out.println("arrayList is empty");
      } else {
         System.out.println("arrayList is not empty");
      }

      // printing all the elements available in arrayList
      System.out.println("ArrayList = " + arrayList);     
   }
}

class Student {
   int rollNo;
   String name;

   Student(int rollNo, String name){
      this.rollNo = rollNo;
      this.name = name;
   }

   @Override
   public String toString() {
      return "[ " + this.rollNo + ", " + this.name + " ]";
   }
   
   @Override
   public boolean equals(Object obj) {
      Student s = (Student)obj;
      return this.rollNo == s.rollNo && this.name.equalsIgnoreCase(s.name);
   }
}

Output

Let us compile and run the above program, this will produce the following result −

arrayList is empty
ArrayList = []
arrayList is not empty
ArrayList = [[ 1, Julie ], [ 2, Robert ], [ 3, Adam ]]
java_util_arraylist.htm
Advertisements