java.util.TreeSet.isEmpty() Method


Advertisements


Description

The isEmpty() method is used to return true if this set contains no elements.

Declaration

Following is the declaration for java.util.TreeSet.isEmpty() method.

public boolean isEmpty()

Parameters

NA

Return Value

The method call returns true if this set contains no elements.

Exception

NA

Example

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

Live Demo
package com.tutorialspoint;

import java.util.TreeSet;

public class TreeSetDemo {
   public static void main(String[] args) {

      // creating a TreeSet 
      TreeSet <Integer>treeadd = new TreeSet<Integer>();

      // checking tree set
      System.out.println("Is the tree set empty: "+treeadd.isEmpty()); 

      // adding elements in the tree set
      System.out.println("Adding elements in the tree set"); 
      treeadd.add(12);
      treeadd.add(11);
      treeadd.add(16);
      treeadd.add(15);

      // checking tree set again
      System.out.println("Is the tree set empty: "+treeadd.isEmpty());  
   }     
}

Let us compile and run the above program, this will produce the following result.

Is the tree set empty: true
Adding elements in the tree set
Is the tree set empty: false

java_util_treeset.htm

Advertisements