TreeSet pollLast() method in Java with Example
The pollLast() method of Java.util.concurrent.TreeSet is an in-built function in Java which returns retrieves and removes the last (highest) element, or returns null if this set is empty.
Syntax:
public E pollLast()
Return Value: The function returns retrieves and removes the last (highest) element, or returns null if this set is empty.
Below programs illustrate the TreeSet.pollLast() method:
Program 1:
// Java program to demonstrate pollLast() // method of TreeSet import java.util.*; class TreeSetpollLastExample1 { public static void main(String[] args) { // Creating a set object TreeSet<Integer> set = new TreeSet<Integer>(); // Adding elements to this set for (int i = 10; i <= 50; i += 10) set.add(i); // Printing the content of the set System.out.println("Contents of the set: " + set); // Retrieving and removing Last element of the set System.out.println("The Last element of the set: " + set.pollLast()); // Printing the content of the set after pollLast() System.out.println("Contents of the set after pollLast: " + set); } } |
Contents of the set: [10, 20, 30, 40, 50] The Last element of the set: 50 Contents of the set after pollLast: [10, 20, 30, 40]
Program 2:
// Java program to demonstrate pollLast() // method of TreeSet import java.util.*; class TreeSetpollLastExample2 { public static void main(String[] args) { // Creating a set object TreeSet<Integer> set = new TreeSet<Integer>(); // Printing the content of the set System.out.println("Contents of the set: " + set); // Retrieving and removing Last element of the set System.out.println("The Last element of the set: " + set.pollLast()); } } |
Contents of the set: [] The Last element of the set: null
Recommended Posts:
- NavigableSet pollLast() method in Java
- LinkedBlockingDeque pollLast() method in Java
- ConcurrentLinkedDeque pollLast() method in Java
- ArrayDeque pollLast() Method in Java
- ConcurrentSkipListSet pollLast() method in Java
- TreeSet contains() Method in Java
- TreeSet add() Method in Java
- TreeSet first() Method in Java
- TreeSet last() Method in Java
- TreeSet size() Method in Java
- TreeSet pollFirst() method in Java
- TreeSet lower() method in Java
- TreeSet containsAll() method in Java with Example
- TreeSet toArray(T[]) method in Java with Example
- TreeSet comparator() Method in Java
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



