Write a Java program to Check whether a TreeSet is empty or not


The code demonstrates how to check if a TreeSet is empty or not in Java. In the EmptyNot class, two TreeSet objects are created: num and emptynum. Integers such as 70, 30, 50, 20, and 60 are added to the num set using the add method.

The isEmpty() method is used to check if a TreeSet is empty or not. In this case, the num.isEmpty() method is used to check if the num set is empty. If the num set is empty, it will print "TreeSet is an Empty" using the System.out.println statement in the if block. Otherwise, if the num set is not empty, it will print "TreeSet is Not an Empty" using the System.out.println statement in the else block. Note that the emptynum set is not used in this code and does not affect the result of the isEmpty() check.

Source Code

import java.io.*;
import java.util.*;
public class EmptyNot
{
	public static void main(String args[])
	{
		TreeSet <Integer> num = new TreeSet <Integer>();
		TreeSet <Integer> emptynum = new TreeSet <Integer>();
		num.add(70);
		num.add(30);
		num.add(50);
		num.add(20);
		num.add(60);
 
		//if(emptynum.isEmpty())
		if(num.isEmpty())
		{
			System.out.println("TreeSet is an Empty");
		}
		else
		{
			System.out.println("TreeSet is Not an Empty");
		}
	}
}

Output

TreeSet is Not an Empty