The isEmpty() method of Java AbstractCollection class


The isEmpty() method of the AbstractCollection class checks whether the collection is empty or not i.e. whether it has zero elements. It returns if the Collectionn has no elements.

The syntax is as follows −

public boolean isEmpty()

To work with AbstractCollection class in Java, import the following package −

import java.util.AbstractCollection;

The following is an example to implement AbstractCollection isEmpty() method in Java −

Example

 Live Demo

import java.util.ArrayList;
import java.util.AbstractCollection;

public class Demo {
   public static void main(String[] args) {
      AbstractCollection<Object> absCollection = new ArrayList<Object>();
      absCollection.add("Laptop");
      absCollection.add("Tablet");
      absCollection.add("Mobile");
      absCollection.add("E-Book Reader");
      absCollection.add("SSD");
      absCollection.add("HDD");
      System.out.println("AbstractCollection = " + absCollection);
      System.out.println("Collection is empty? = " + absCollection.isEmpty());
   }
}

Output

AbstractCollection = [Laptop, Tablet, Mobile, E-Book Reader, SSD, HDD]
Collection is empty? = false

Let us see another example −

Example

 Live Demo

import java.util.ArrayList;
import java.util.AbstractCollection;

public class Demo {
   public static void main(String[] args) {
      AbstractCollection<Object> absCollection = new ArrayList<Object>();
      System.out.println("AbstractCollection = " + absCollection);
      System.out.println("Collection is empty? = " + absCollection.isEmpty());
   }
}

Output

AbstractCollection = []
Collection is empty? = true

Updated on: 30-Jul-2019

236 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements