The isEmpty() method of Java AbstractCollection class

Java 8Object Oriented ProgrammingProgramming

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
raja
Published on 27-Mar-2019 13:13:45
Advertisements