TreeSet iterator() method in java

Let’s learn TreeSet iterator() method in java.

TreeSet iterator() method in java

iterator() method of TreeSet class returns an iterator over the elements in this set in ascending order.

Syntax:

public Iterator<E> iterator()

Also read – TreeSet in java

Now let’s see example on TreeSet iterator() method.

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() + " ");
      }
   }
}


Output:

TreeSet in ascending order:
21
23
52
63