Iterate through elements of TreeSet - Java Collection Framework

Java examples for Collection Framework:TreeSet

Description

Iterate through elements of TreeSet

Demo Code


import java.util.TreeSet;
import java.util.Iterator;
 
public class Main {
 
  public static void main(String[] args) {
    TreeSet tSet = new TreeSet();
   //from   w ww . j a v a  2s.c o m
    tSet.add(new Integer("1"));
    tSet.add(new Integer("2"));
    tSet.add(new Integer("3"));
   
    Iterator itr = tSet.iterator();
   
    System.out.println("TreeSet contains : ");
    while(itr.hasNext())
      System.out.println(itr.next());
  }
}

Result


Related Tutorials