Java TreeSet iterator() Method

The iterator() method of TreeSet class is used to get the elements in the increasing/ascending order from the Set.

Syntax:

Return:

It returns an iterator of elements in increasing order.

Example 1

Output:

TreeSet Element: 1
TreeSet Element : 2
TreeSet Element : 5
TreeSet Element : 8
TreeSet Element : 10

Example 2

Output:

TreeSet Element : A
TreeSet Element : B
TreeSet Element : a
TreeSet Element : b

Example 3

Output:

TreeSet: A 1
TreeSet: B 2
TreeSet: a 3
TreeSet: b 4