The isEmpty() method of Java AbstractCollection is used to check and verify if a Collection is empty or not. It returns True if the Collection is empty else it returns False.
Syntax:
AbstractCollection.isEmpty()
Parameters: The method does not take any parameter.
Return Value: The function returns True if the collection is empty else it returns False.
Below program illustrate the use of AbstractCollection.isEmpty() method:
Program 1:
// Java code to illustrate isEmpty() method import java.util.*; import java.util.AbstractCollection; public class AbstractCollectionDemo { public static void main(String args[]) { // Creating an empty Collection AbstractCollection<String> abs = new TreeSet<String>(); // Use add() method to add // elements into the Collection abs.add("Welcome"); abs.add("To"); abs.add("Geeks"); abs.add("4"); abs.add("Geeks"); abs.add("TreeSet"); // Displaying the Collection System.out.println("Collection: " + abs); // Check for the empty collection System.out.println("Is the collection empty? " + abs.isEmpty()); // Clearing the collection // using clear() method abs.clear(); // Again Checking for the empty collection System.out.println("Is the collection empty? " + abs.isEmpty()); } } |
Collection: [4, Geeks, To, TreeSet, Welcome] Is the collection empty? false Is the collection empty? true
Program 2:
// Java code to illustrate isEmpty() method import java.util.*; import java.util.AbstractCollection; public class AbstractCollectionDemo { public static void main(String args[]) { // Creating an empty Collection AbstractCollection<String> abs = new ArrayList<String>(); // Use add() method to add // elements into the Collection abs.add("Welcome"); abs.add("To"); abs.add("Geeks"); abs.add("4"); abs.add("Geeks"); abs.add("ArrayList"); // Displaying the Collection System.out.println("Collection: " + abs); // Check for the empty collection System.out.println("Is the collection empty? " + abs.isEmpty()); // Clearing the collection using clear() method abs.clear(); // Again Checking for the empty collection System.out.println("Is the collection empty? " + abs.isEmpty()); } } |
Collection: [Welcome, To, Geeks, 4, Geeks, ArrayList] Is the collection empty? false Is the collection empty? true
Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.
Recommended Posts:
- AbstractCollection clear() Method in Java with Examples
- AbstractCollection addAll() Method in Java with Examples
- AbstractCollection add() Method in Java with Examples
- AbstractCollection size() Method in Java with Examples
- AbstractCollection remove() Method in Java with Examples
- AbstractCollection contains() Method in Java with Examples
- AbstractCollection containsAll() Method in Java with Examples
- AbstractCollection toArray() Method in Java with Examples
- AbstractCollection retainAll() method in Java with Examples
- AbstractCollection toString() method in Java with Examples
- AbstractCollection in Java with Examples
- AbstractCollection removeAll() method in Java with Example
- WeakHashMap isEmpty() Method in Java with Examples
- AbstractMap isEmpty() Method in Java with Examples
- Collection isEmpty() method in Java with Examples
- List isEmpty() method in Java with Examples
- ConcurrentLinkedDeque isEmpty() method in Java with Examples
- Set isEmpty() method in Java with Examples
- Map isEmpty() Method in Java with Examples
- ConcurrentSkipListMap isEmpty() method in Java with Examples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

