ConcurrentSkipListSet pollLast() method in Java

The pollLast() method of java.util.concurrent.ConcurrentSkipListSet 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 ConcurrentSkipListSet.pollLast() method:

Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate pollLast()
// method of ConcurrentSkipListSet
  
import java.util.concurrent.*;
  
class ConcurrentSkipListSetpollLastExample1 {
    public static void main(String[] args)
    {
        // Creating a set object
        ConcurrentSkipListSet<Integer> Lset = new ConcurrentSkipListSet<Integer>();
  
        // Adding elements to this set
        for (int i = 10; i <= 50; i += 10)
            Lset.add(i);
  
        // Printing the content of the set
        System.out.println("Contents of the set: " + Lset);
  
        // Retrieving and removing Last element of the set
        System.out.println("The Last element of the set: " + Lset.pollLast());
  
        // Printing the content of the set after pollLast()
        System.out.println("Contents of the set after pollLast: " + Lset);
    }
}

chevron_right


Output:

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:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate pollLast()
// method of ConcurrentSkipListSet
  
import java.util.concurrent.*;
  
class ConcurrentSkipListSetpollLastExample2 {
    public static void main(String[] args)
    {
        // Creating a set object
        ConcurrentSkipListSet<Integer> Lset = new ConcurrentSkipListSet<Integer>();
  
        // Printing the content of the set
        System.out.println("Contents of the set: " + Lset);
  
        // Retrieving and removing Last element of the set
        System.out.println("The Last element of the set: " + Lset.pollLast());
    }
}

chevron_right


Output:

Contents of the set: []
The Last element of the set: null

Reference:
https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/ConcurrentSkipListSet.html#pollLast–



My Personal Notes arrow_drop_up

Coder Machine Learner Social Activist Vocalist

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.