Skip to content
Related Articles
Open in App
Not now

Related Articles

NavigableSet pollLast() method in Java

Improve Article
Save Article
  • Last Updated : 29 Sep, 2018
Improve Article
Save Article

The pollLast() method of NavigableSet interface in Java is used to retrieve and remove the last (highest) element, or returns null if this set is empty.

Syntax:

E pollLast()

Where, E is the type of elements maintained by this Set container.

Parameters: This function does not accepts any parameter.

Return Value: It returns the last (highest) element, or returns null if this set is empty.

Below programs illustrate the pollLast() method in Java:

Program 1: NavigableSet with integer elements.




// A Java program to demonstrate
// pollLast() method of NavigableSet
  
import java.util.NavigableSet;
import java.util.TreeSet;
  
public class GFG {
    public static void main(String[] args)
    {
        NavigableSet<Integer> ns = new TreeSet<>();
        ns.add(0);
        ns.add(1);
        ns.add(2);
        ns.add(3);
        ns.add(4);
        ns.add(5);
        ns.add(6);
  
        System.out.println("Greatest(last) element removed is : "
                                               + ns.pollLast());
    }
}

Output:

Greatest(last) element removed is : 6

Program 2: NavigableSet with string elements.




// A Java program to demonstrate
// pollLast() method of NavigableSet
  
import java.util.NavigableSet;
import java.util.TreeSet;
  
public class GFG {
    public static void main(String[] args)
    {
        NavigableSet<String> ns = new TreeSet<>();
        ns.add("A");
        ns.add("B");
        ns.add("C");
        ns.add("D");
        ns.add("E");
        ns.add("F");
        ns.add("G");
  
        System.out.println("Greatest(last) element removed is : " 
                                                + ns.pollLast());
    }
}

Output:

Greatest(last) element removed is : G

Reference: https://docs.oracle.com/javase/10/docs/api/java/util/NavigableSet.html#pollLast(E)


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!