TreeSet pollFirst() in Java

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

Syntax:

K java.util.TreeSet.pollFirst()

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

Parameters: NA

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

Exceptions: NA

Approach

Java

import java.util.TreeSet;

public class TreeSetpollFirst {
    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.pollFirst());

    }
}

Output:

C++


No comments:

Post a Comment