Java TreeSet pollLast() Method
Example
Remove the last item of a sorted set:
import java.util.TreeSet;
public class Main {
public static void main(String[] args) {
TreeSet<String> cars = new TreeSet<String>();
cars.add("Volvo");
cars.add("BMW");
cars.add("Ford");
cars.add("Mazda");
System.out.println(cars.pollLast());
System.out.println(cars);
}
}
Definition and Usage
The pollLast() method removes the last (highest) item from the set, and returns it.
If the set is empty, the method returns null.
Tip: Use the last() method to get the last item without removing it.
Syntax
public T pollLast()
T refers to the data type of items in the set.
Parameter Values
None.
Technical Details
| Returns: | The removed item, or null if the set is empty. |
|---|
Related Pages
❮ TreeSet Methods