iterate treeset in java code example

Example: TreeSet iterator() method in java

import java.util.Iterator;
import java.util.TreeSet;
public class TreeSetIteratorMethodExample
{
   public static void main(String[] args)
   {
      TreeSet<Integer> ts = new TreeSet<Integer>();
      ts.add(23);
      ts.add(52);
      ts.add(63);
      ts.add(21);
      // create ascending iterator
      Iterator<Integer> iterate = ts.iterator();
      // print TreeSet
      System.out.println("TreeSet in ascending order: ");
      while(iterate.hasNext())
      {
         System.out.println(iterate.next() + " ");
      }
   }
}

Tags:

Java Example