Saturday, October 11, 2025
HomeLanguagesJavaNavigableSet pollFirst() method in Java

NavigableSet pollFirst() method in Java

The pollFirst() method of NavigableSet interface in Java is used to retrieves and removes the first (lowest) element, or returns null if this set is empty.

Syntax:

E pollFirst()

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

Parameters: This function does not accepts any parameter.

Return Value: It returns returns null if this set is empty.

Below programs illustrate the pollFirst() method in Java:

Program 1: NavigableSet with integer elements.




// A Java program to demonstrate
// pollFirst() 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("First lowest element removed is : " 
                                              + ns.pollFirst());
    }
}


Output:

First lowest element removed is : 0

Program 2: NavigableSet with string elements.




// A Java program to demonstrate
// pollFirst() 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("First lowest element removed is : " 
                                              + ns.pollFirst());
    }
}


Output:

First lowest element removed is : A

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

RELATED ARTICLES

Most Popular

Dominic
32352 POSTS0 COMMENTS
Milvus
87 POSTS0 COMMENTS
Nango Kala
6720 POSTS0 COMMENTS
Nicole Veronica
11885 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11941 POSTS0 COMMENTS
Shaida Kate Naidoo
6840 POSTS0 COMMENTS
Ted Musemwa
7103 POSTS0 COMMENTS
Thapelo Manthata
6794 POSTS0 COMMENTS
Umr Jansen
6794 POSTS0 COMMENTS