Java TreeSet isEmpty() Method Example

The isEmpty() method of java.util.TreeSet class will tests if the TreeSet has any elements or not.

Signature

public boolean isEmpty() 

This method checks if the TreeSet has any elements in it or not, returns true if and only if the TreeSet has no components,else it will return false.

Example

The following example shows the usage of java.util.TreeSet.isEmpty() method.

import java.util.TreeSet;

public class IsEmptyMethodTreeSetExample 
{
    public static void main(String args[])
    {
     // create an empty TreeSet
        TreeSet treeSet1 = new TreeSet();
        
        // use add() method to add elements to the TreeSet
        treeSet1.add("Element1");
        treeSet1.add("Element2");
        treeSet1.add("Element3");
        
        //Check if treeSet1 is empty or not 
        System.out.println("Is \"treeSet1\" empty : "+treeSet1.isEmpty());
    }
}

Output

Is "treeSet1" empty : false

Leave a Reply

Your email address will not be published. Required fields are marked *