Collection isEmpty() method in Java with Examples

29 Nov, 2018
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 not take any parameter and does not returns any value.
Syntax:
Collection.isEmpty()

Parameters: This method do not accept any parameter
Return Value: This method does not return any value.
Below examples illustrate the Collection isEmpty() method:
Example 1: Using LinkedList Class
// 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 in 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());    }}
The 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 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 into 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());    }}
ArrayDeque: [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 code to illustrate isEmpty() method  import java.io.*;import java.util.*;  public class ArrayListDemo {    public static void main(String[] args)    {          // create an empty array list with an initial capacity        Collection<Integer>            arrlist = new ArrayList<Integer>(5);          // use add() method to add elements in the list        arrlist.add(15);        arrlist.add(20);        arrlist.add(25);          // prints all the elements available in 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());    }}
ArrayList: [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–
Java - util package
Java-Collections
Java-Functions
Java
Java
Java-Collections
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Stream In Java
Introduction to Java
Constructors in Java
Exceptions in Java
Generics in Java
Functional Interfaces in Java
Java Programming Examples
Strings in Java
Differences between JDK, JRE and JVM
Abstraction in Java