Collection isEmpty() method in Java with Examples Last Updated : 24 Jul, 2024 Comments Improve Suggest changes Like Article Like Report The isEmpty() of java.util.Collection interface is used to check if the Collection upon which it is called is empty or not. This method does return a boolean value indicating whether the collection is empty or not. Here's the correct information for the isEmpty() method: Syntax:boolean isEmpty()Parameters:This method does not accept any parameter.Return Value:This method returns a boolean value, which indicates whether the collection is empty or not. The below examples illustrate the Collection isEmpty() method: Example 1: Using LinkedList Class Java // Java code to illustrate boolean isEmpty() method import java.io.*; import java.util.*; public class GFG { public static void main(String[] args) { // Creating an empty LinkedList Collection<String> list = new LinkedList<String>(); // Use add() method to add elements to the list list.add("Geeks"); list.add("for"); list.add("Geeks"); // Output the present list System.out.println("The list is: " + list); // Check if list is empty using isEmpty() method System.out.println("Is the LinkedList empty: " + list.isEmpty()); // Clearing the LinkedList list.clear(); // Printing the new list System.out.println("The new List is: " + list); // Check if list is empty using isEmpty() method System.out.println("Is the LinkedList empty: " + list.isEmpty()); } } OutputThe list is: [Geeks, for, Geeks] Is the LinkedList empty: false The new List is: [] Is the LinkedList empty: true Example 2: Using ArrayDeque Class Java // Java code to illustrate isEmpty() method import java.util.*; public class ArrayDequeDemo { public static void main(String[] args) { // Creating an empty ArrayDeque Collection<String> de_que = new ArrayDeque<String>(); // Use add() method to add elements to the Deque de_que.add("Welcome"); de_que.add("To"); de_que.add("Geeks"); de_que.add("4"); de_que.add("Geeks"); // Displaying the ArrayDeque System.out.println("ArrayDeque: " + de_que); // Check if ArrayDeque is empty using isEmpty() method System.out.println("Is the ArrayDeque empty: " + de_que.isEmpty()); // Clearing the ArrayDeque de_que.clear(); // Printing the new ArrayDeque System.out.println("The new ArrayDeque is: " + de_que); // Check if ArrayDeque is empty using isEmpty() method System.out.println("Is the ArrayDeque empty: " + de_que.isEmpty()); } } OutputArrayDeque: [Welcome, To, Geeks, 4, Geeks] Is the ArrayDeque empty: false The new ArrayDeque is: [] Is the ArrayDeque empty: true Example 3: Using ArrayList Class Java // Java code to illustrate isEmpty() method import java.io.*; import java.util.*; public class ArrayListDemo { public static void main(String[] args) { // Create an empty ArrayList with an initial capacity Collection<Integer> arrlist = new ArrayList<Integer>(5); // Use add() method to add elements to the list arrlist.add(15); arrlist.add(20); arrlist.add(25); // Print all the elements available in the list System.out.println("ArrayList: " + arrlist); // Check if list is empty using isEmpty() method System.out.println("Is the ArrayList empty: " + arrlist.isEmpty()); // Clearing the ArrayList arrlist.clear(); // Printing the new ArrayList System.out.println("The new ArrayList is: " + arrlist); // Check if ArrayList is empty using isEmpty() method System.out.println("Is the ArrayList empty: " + arrlist.isEmpty()); } } OutputArrayList: [15, 20, 25] Is the ArrayList empty: false The new ArrayList is: [] Is the ArrayList empty: true Reference:https://docs.oracle.com/javase/9/docs/api/java/util/Collection.html#isEmpty-- Comment More infoAdvertise with us Next Article Collection isEmpty() method in Java with Examples R RishabhPrabhu Follow Improve Article Tags : Java Java-Collections Java - util package Java-Functions Practice Tags : JavaJava-Collections Similar Reads AbstractCollection isEmpty() Method in Java with Examples 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 T 2 min read Collection add() Method in Java with Examples The add(E element) of java.util.Collection interface is used to add the element 'element' to this collection. This method returns a boolean value depicting the successfulness of the operation. If the element was added, it returns true, else it returns false. Syntax: Collection.add(E element) Paramet 4 min read Collections min() method in Java with Examples min(Collection<? extends T> coll) The min() method of java.util.Collections class is used to return the minimum element of the given collection, according to the natural ordering of its elements. All elements in the collection must implement the Comparable interface. Furthermore, all elements 4 min read Collection clear() method in Java with Examples The collection clear() method of Java Collection Interface clears the Collection upon which it is called. After this method is called, the collection will be empty as it removes all the elements from the collection. This method does not take any parameter and does not return any value. Example: Java 3 min read CompoundName isEmpty() method in Java with Examples The isEmpty() method of a javax.naming.CompoundName class is used to check this compound name object is empty or not. A compound name is said to be empty if it has zero components. Syntax: public boolean isEmpty() Parameters: This method accepts nothing. Return value: This method returns true if thi 2 min read Like