TreeSet isEmpty() method in java

Let’s learn TreeSet isEmpty() method in java.

TreeSet isEmpty() method in java

isEmpty() method of TreeSet class returns true if this set contains no elements.

Syntax:

public boolean isEmpty()

Parameters:

isEmpty() method do not take any parameters.

Also read – TreeSet in java

Now let’s see example on TreeSet isEmpty() method.

import java.util.TreeSet;
public class TreeSetIsEmptyMethodExample
{
   public static void main(String[] args)
   {
      TreeSet<String> ts = new TreeSet<String>();
      ts.add("violet");
      ts.add("green");
      ts.add("red");
      ts.add("yellow");
      ts.add("blue");
      ts.add("indigo");
      System.out.println("Given TreeSet: " + ts);
      // check for empty TreeSet
      System.out.println("Is TreeSet empty? " + ts.isEmpty());
      // clear TreeSet using clear() method
      ts.clear();
      // again check for empty TreeSet
      System.out.println("Is TreeSet empty? " + ts.isEmpty());
   }
}


Output:

Given TreeSet: [blue, green, indigo, red, violet, yellow]
Is TreeSet empty? false
Is TreeSet empty? true