This Java Example shows how to remove all elements exists in TreeSet.
isEmpty and clear in Java TreeSet Example
This Java Example shows how to remove all elements exists in TreeSet. To remove all the elements exists in TreeSet We use clear() method of TreeSet class. It removes all the elements stored in the specified object within the collection. To check that specified TreeSet object is empty or not we use boolean isEmpty() method of the TreeSet class. This method returns true if the object is empty otherwise returns false.
Example:- RemoveElementOfTreeSet.java
package devmanuals.com; import java.util.*; public class RemoveElementOfTreeSet { public static void main(String[] args) { TreeSet st = new TreeSet(); st.add("Gyan"); st.add("Rohit"); st.add("Anand"); st.add("Arunesh"); System.out.println("The size of the tree set is :" + st.size()); st.clear(); System.out.println("The size of the tree set after removing is :" + st.size()); boolean bool = st.isEmpty(); System.out.println("Is the TreeSet is empty: " + bool); } }
Output:-
The size of the tree set is :4 The size of the tree set after removing is :0 Is the TreeSet is empty: true |
[ 0 ] Comments