Java Program to Check Whether a TreeSet Is Empty or Not

You are currently viewing Java Program to Check Whether a TreeSet Is Empty or Not

Java program to check whether a TreeSet is empty or not

// Java program to check whether a TreeSet 
// is empty or not

import java.io.*;
import java.util.*;

public class Main {
  public static void main(String args[]) {
    TreeSet < Integer > tree = new TreeSet < Integer > ();
    TreeSet < Integer > empty = new TreeSet < Integer > ();

    tree.add(10);
    tree.add(20);
    tree.add(30);
    tree.add(40);
    tree.add(50);
    tree.add(60);

    if (tree.isEmpty()) {
      System.out.println("TreeSet 'tree' is an empty collection.");
    } else {
      System.out.println("TreeSet 'tree' is not an empty collection.");
    }

    if (empty.isEmpty()) {
      System.out.println("TreeSet 'empty' is an empty collection.");
    } else {
      System.out.println("TreeSet 'empty' is not an empty collection.");
    }
  }
}

Output

TreeSet 'tree' is not an empty collection.
TreeSet 'empty' is an empty collection.

Ranjith

Hi, I'm Manoj a full-time Blogger, YouTuber, Affiliate Marketer, & founder of Coding Diksha. Here, I post about programming to help developers.

Leave a Reply