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:
- 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:
// Java program to loop over TreeSetimportjava.util.Iterator;importjava.util.TreeSet;publicclassTreeSetExample {publicstaticvoidmain(String[] args){TreeSet<Integer>ts =newTreeSet<Integer>();ts.add(10);ts.add(61);ts.add(87);ts.add(39);// Create an Iterator over the TreeSetIterator<Integer> iterator = ts.iterator();// Loop over the TreeSet values// and print the valuesSystem.out.print("TreeSet: ");while(iterator.hasNext())System.out.print(iterator.next()+", ");System.out.println();}}chevron_rightfilter_noneOutput:TreeSet: 10, 39, 61, 87,
- 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:
// Java program to loop over TreeSetimportjava.util.Iterator;importjava.util.TreeSet;publicclassTreeSetExample {publicstaticvoidmain(String[] args){TreeSet<Integer>ts =newTreeSet<Integer>();ts.add(10);ts.add(61);ts.add(87);ts.add(39);// Loop over the TreeSet values// and print the valuesSystem.out.print("TreeSet: ");for(Integer value : ts)System.out.print(value+", ");System.out.println();}}chevron_rightfilter_noneOutput:TreeSet: 10, 39, 61, 87,
Recommended Posts:
- TreeSet in Java
- TreeSet in Java
- TreeSet first() Method in Java
- TreeSet contains() Method in Java
- TreeSet add() Method in Java
- HashSet vs TreeSet in Java
- TreeSet last() Method in Java
- TreeSet size() Method in Java
- TreeSet remove() Method in Java
- TreeSet iterator() Method in Java
- TreeSet comparator() Method in Java
- TreeSet clone() Method in Java
- TreeSet pollLast() method in Java with Example
- TreeSet addAll() Method in Java
- TreeSet headSet() Method in Java
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.



