The isEmpty() method of CopyOnWriteArrayList method in Java


To check whether the list is empty or not, use the isEmpty() method. TRUE is returned if the list is empty, else FALSE is the return value.

The syntax is as follows

boolean isEmpty()

To work with CopyOnWriteArrayList class, you need to import the following package

import java.util.concurrent.CopyOnWriteArrayList;

The following is an example to implement CopyOnWriteArrayList class isEmpty() method in Java

Example

 Live Demo

import java.util.concurrent.CopyOnWriteArrayList;
public class Demo {
   public static void main(String[] args) {
      CopyOnWriteArrayList<Integer> arrList = new CopyOnWriteArrayList<Integer>();
      arrList.add(30);
      arrList.add(40);
      arrList.add(60);
      arrList.add(70);
      arrList.add(90);
      arrList.add(100);
      arrList.add(120);
      System.out.println("CopyOnWriteArrayList = " + arrList);
      System.out.println("Is the CopyOnWriteArrayList empty? " + arrList.isEmpty());
   }
}

Output

CopyOnWriteArrayList = [30, 40, 60, 70, 90, 100, 120]
Is the CopyOnWriteArrayList empty? False

Let us see another example

Example

 Live Demo

import java.util.concurrent.CopyOnWriteArrayList;
public class Demo {
   public static void main(String[] args) {
      CopyOnWriteArrayList<Integer> arrList = new CopyOnWriteArrayList<Integer>();
      arrList.add(30);
      arrList.add(40);
      arrList.add(60);
      arrList.add(70);
      arrList.add(90);
      arrList.add(100);
      arrList.add(120);
      System.out.println("CopyOnWriteArrayList = " + arrList);
      System.out.println("Is the CopyOnWriteArrayList empty? " + arrList.isEmpty());
      arrList.clear();
      System.out.println("Updated CopyOnWriteArrayList = " + arrList);
      System.out.println("Is the updated CopyOnWriteArrayList empty? " + arrList.isEmpty());
   }
}

Output

CopyOnWriteArrayList = [30, 40, 60, 70, 90, 100, 120]
Is the CopyOnWriteArrayList empty? false
Updated CopyOnWriteArrayList = []
Is the updated CopyOnWriteArrayList empty? True

Updated on: 30-Jul-2019

64 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements