How to check TreeSet Empty Or not

To check TreeSet is empty or not we use isEmpty() method . TreeSet isEmpty() method returns true if this set contains no elements.This example shows how to check TreeSet is empty or not using isEmpty() method . Let’s see this by code

import java.util.Set;
import java.util.TreeSet;

public class IsEmpty {

public static void main(String[] args) {

Set<String> ts = new TreeSet<String>();
ts.add("mumbai");
ts.add("delhi");
ts.add("kolkata");
ts.add("chandigarh");
ts.add("dehradun");

// print treeset
System.out.println(" ts ="+ts);

System.out.println("Is Empty = "+ts.isEmpty());

Set<String> set = new TreeSet<String>();

System.out.println("Is Empty = "+set.isEmpty());

}

}

Output:

ts =[chandigarh, dehradun, delhi, kolkata, mumbai]

Is Empty = false

Is Empty = true

You can learn more about TreeSet from Java TreeSet Example .

Leave a Reply

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

+ 60 = 65