Java TreeSet pollLast() Method

The pollLast()method of Java TreeSet class is used to find the largest element and remove it from the set. If the set is empty, it returns null.

Syntax:

Specified by:

pollLast in interface NavigableSet<E> : It is used to find the largest element and remove it from the set, if the set is empty it returns null.

Return:

It returns the last element of the set, else null if it is empty.

Example 1

Output:

TreeSet: [ 10, 13, 22, 34, 45 ]
Removed element using pollLast() : 45
After removing element from TreeSet: [ 10, 13, 22, 34 ]

Example 2

Output:

TreeSet: [A, P, S, g]
Removed element using pollLast() : g
After removing element from TreeSet: [A, P, S]

Example 3

Output:

TreeSet: [ 2, 3, 5, 7, K, M, V, c ]
Removed element using pollLast() : c
After removing element from TreeSet: [3, 5, 7, K, M, V]