Java TreeSet first() method

The first() method of TreeSet class is used to find smallest element of the set.

Syntax:

Return:

Returns smallest element of the set

Example 1

Output:

TreeSet: [ 2, 3, 5, 7, 10 ]
Smallest element in TreeSet: 2

Example 2

Output:

TreeSet: [ D, P, g, q ]
Smallest element in TreeSet: D

Example 3

Output:

TreeSet: [ 1, 2, 3, 5, K, M, V, c ]
Largest element in TreeSet: 1