![]() |
VOOZH | about |
In Java, to iterate over the elements of a TreeSet in their Natural Order, one must either use a custom comparator provided or traverse the elements in ascending order based on their natural ordering. A TreeSet in Java keeps up with its components in arranged requests.
In this article, we will learn how to iterate over the elements in a TreeSet in natural order in Java.
for (Integer Value : treeSet)
{
System.out.print(Value);
}
Below is the implementation using an Enhanced For loop to iterate over the elements in a TreeSet in natural order utilizing a basic Java program:
Iterating over TreeSet in natural order: 1 2 5 8
TreeSet is created to store integers. TreeSet.for-each loop) to print them in natural order.