Java TreeSet.clear()

Syntax

TreeSet.clear() has the following syntax.

public void clear()

Example

In the following code shows how to use TreeSet.clear() method.


import java.util.Iterator;
import java.util.TreeSet;
// ww  w .  j a va  2  s .  c  o  m
public class Main {
   public static void main(String[] args) {
     
     TreeSet<Integer> treeadd = new TreeSet<Integer> ();
     
     treeadd.add(12);
     treeadd.add(13);
     treeadd.add(14);
     treeadd.add(15);
     
     Iterator<Integer> iterator = treeadd.iterator();
     
     while (iterator.hasNext()){
        System.out.println(iterator.next());
     }
     
     treeadd.clear();
     System.out.println(treeadd.size());      
   }     
}

The code above generates the following result.