TreeSet pollFirst() method in Java

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

E pollFirst()

Where, E is the type of elements maintained by this TreeSet container.
Parameters: This function does not accepts any parameter.
Return Type:It returns the first element of the Treeset if TreeSet is not empty else returns null.
Below programs illustrate the pollFirst() method in Java:

Example 1

filter_none

edit
close

play_arrow

link
brightness_4
code

// A Java program to demonstrate
// pollFirst() method of TreeSet
  
import java.util.TreeSet;
  
public class GFG {
    public static void main(String[] args)
    {
        // creating TreeSet
        TreeSet<Integer> ts = new TreeSet<Integer>();
  
        // adding element to the TreeSet
        ts.add(0);
        ts.add(1);
        ts.add(2);
        ts.add(3);
        ts.add(4);
        ts.add(5);
        ts.add(6);
  
        // before removing element
        System.out.println("Before removing "
                   "element from TreeSet: " + ts);
  
        // first element is removed
        System.out.println("First lowest element "
                 "removed is : " + ts.pollFirst());
        System.out.println("After removing element"
                             " from TreeSet: " + ts);
    }
}

chevron_right


Output:

Before removing element from TreeSet: [0, 1, 2, 3, 4, 5, 6]
First lowest element removed is : 0
After removing element from TreeSet: [1, 2, 3, 4, 5, 6]

Example 2

filter_none

edit
close

play_arrow

link
brightness_4
code

// A Java program to demonstrate
// pollFirst() method of TreeSet
  
import java.util.TreeSet;
  
public class GFG {
    public static void main(String[] args)
    {
        // creating TreeSet
        TreeSet<String> ts = new TreeSet<String>();
  
        // removing element when TreeSet
        // is empty
        System.out.println("pollFirst() method when "
             "TreeSet is empty  :  " + ts.pollFirst());
  
        // adding element to the TreeSet
        ts.add("Geeks");
        ts.add("for");
        ts.add("Geek");
  
        // before removing element
        System.out.println("Before removing element from"
                                      " TreeSet: " + ts);
  
        // first element is removed
        System.out.println("First lowest element removed is : "
                           + ts.pollFirst());
        System.out.println("After removing element "
                               "from TreeSet: " + ts);
    }
}

chevron_right


Output:

pollFirst() method when TreeSet is empty  :  null
Before removing element from TreeSet: [Geek, Geeks, for]
First lowest element removed is : Geek
After removing element from TreeSet: [Geeks, for]

If TreeSet is empty, then this method returns null.
Example 3

filter_none

edit
close

play_arrow

link
brightness_4
code

// A Java program to demonstrate
// pollFirst() method of TreeSet
  
import java.util.TreeSet;
  
public class GFG {
    public static void main(String[] args)
    {
        // creating TreeSet
        TreeSet<Integer> ts = new TreeSet<Integer>();
  
        // before removing element
        System.out.println("Before removing "
                   "element from TreeSet: " + ts);
  
        // first element is removed
        System.out.println("First lowest element "
                 "removed is : " + ts.pollFirst());
        System.out.println("After removing element"
                             " from TreeSet: " + ts);
    }
}

chevron_right


Output:

Before removing element from TreeSet: []
First lowest element removed is : null
After removing element from TreeSet: []


My Personal Notes arrow_drop_up

All powers are within you You can do anything and everything Believe in that

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.