How to loop over TreeSet in Java

Given a TreeSet in Java, the task is to loop over this TreeSet.

TreeSet provides an implementation of the SortedSet Interface and SortedSet extends Set Interface. It behaves like simple set with the exception that it stores elements in sorted format.

Following are the features of TreeSet.



  • TreeSet uses tree data structure for storage.
  • Objects are stored in sorted, ascending order. But we can iterate in descending order using method TreeSet.descendingIterator().
  • Access and retrieval times are very fast which make TreeSet an excellent choice for storage of large volume of data in sorted format.
  • TreeSet doesn’t use hashCode() and equals() methods to compare it’s elements. It uses compare() (or compareTo()) method to determine the equality of two elements.

Below are the two methods to loop over the TreeSet in Java:

  1. Using Iterator: Iterator can be created over the TreeSet objects. Hence this iterator can be used to traverse or loop through the TreeSet.

    Syntax:

    Iterator iterator = ts.iterator();
    while (iterator.hasNext())
        System.out.print(iterator.next());
    

    Program:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // Java program to loop over TreeSet
      
    import java.util.Iterator;
    import java.util.TreeSet;
      
    public class TreeSetExample {
        public static void main(String[] args)
        {
            TreeSet<Integer>
                ts = new TreeSet<Integer>();
            ts.add(10);
            ts.add(61);
            ts.add(87);
            ts.add(39);
      
            // Create an Iterator over the TreeSet
            Iterator<Integer> iterator = ts.iterator();
      
            // Loop over the TreeSet values
            // and print the values
            System.out.print("TreeSet: ");
            while (iterator.hasNext())
                System.out.print(iterator.next()
                                 + ", ");
            System.out.println();
        }
    }

    chevron_right

    
    

    Output:

    TreeSet: 10, 39, 61, 87,
    
  2. Using Enhanced For loop: Enhanced For loop can be used to loop over the TreeSet in the below manner.

    Syntax:

    for (Integer value : ts)
        System.out.print(value);
    }
    

    Program:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // Java program to loop over TreeSet
      
    import java.util.Iterator;
    import java.util.TreeSet;
      
    public class TreeSetExample {
        public static void main(String[] args)
        {
            TreeSet<Integer>
                ts = new TreeSet<Integer>();
            ts.add(10);
            ts.add(61);
            ts.add(87);
            ts.add(39);
      
            // Loop over the TreeSet values
            // and print the values
            System.out.print("TreeSet: ");
            for (Integer value : ts)
                System.out.print(value
                                 + ", ");
            System.out.println();
        }
    }

    chevron_right

    
    

    Output:

    TreeSet: 10, 39, 61, 87,
    


My Personal Notes arrow_drop_up

Technical Content Engineer at GeeksForGeeks

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.