How to clear the TreeSet and check whether it is Empty
Previous | Home | Next |
This Example will show how to clear the TreeSet and how to check whether the set is Empty or not?
In this Example we uses the Two methods Clear() and isEmpty() .To clear the TreeSet and to check whether it is empty or not.
void clear() and isEmpty () are the two methods which we are used to clear the TreeSet and to check whether it is Empty or not respectively.
After adding value to the TreeSet . call the void clear method through the object Tst. it will clear the TreeSet.
After that to check whether the TreeSet is Empty or not call the isEmpty method .which show the result in the boolean True/false
package Example;
import java.util.Iterator;
import java.util.TreeSet;
import java.util.SortedSet;
public class TreeSetSubSet {
/**
* @param args
*/
/**
* @param args
*/
public static void main(String[] args) {
TreeSet Tst =new TreeSet();
Tst.add(new Integer("10"));
Tst.add(new Integer("17"));
Tst.add(new Integer("15"));
Tst.add(new Integer("19"));
Tst.add(new Integer("12"));
Tst.add(new Integer("13"));
System.out.println(" The elements of the TreeSet before removal are:" +Tst);
Tst.clear();
//boolean sortedSet = Tst.retainAll(Tst);
//boolean itr=Tst.add(new Integer ("16"));
//System.out.println("The TreeSet contains :");
//while(itr.hasNext())
{
// System.out.println(itr.next());
}
System.out.println(" After removal:" +Tst);
System.out.println("The TreeSet is Empty:" + Tst.isEmpty());
// TODO Auto-generated method stub
}
}
The elements of the TreeSet before removal are:[10, 12, 13, 15, 17, 19]
After removal:[]
The TreeSet is Empty:true
Previous | Home | Next |