TreeSet pollLast() in Java

pollLast(): This method is available in java.util.TreeSet class of Java.

Syntax:

K java.util.TreeSet.pollLast()

This method retrieves and removes the last (highest) element, or returns null if this set is empty.

Parameters: NA

Returns: the last element, or null if this set is empty.

Exceptions: NA

Approach

Java

import java.util.TreeSet;

public class TreeSetpollLast {
    public static void main(String[] args) {

        TreeSet<String> treeSet = new TreeSet<String>();

        treeSet.add("Hello");
        treeSet.add("Java");
        treeSet.add("Program");
        treeSet.add("C++");

        System.out.println(treeSet.pollLast());

    }
}

Output:

Program


No comments:

Post a Comment