Java TreeSet last() method

The last() method of TreeSet class is used to find largest element of the set.

Syntax:

Return:

Returns largest element of the set

Example 1

Output:

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

Example 2

Output:

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

Example 3

Output:

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