TreeSet pollFirst() method in Java - GeeksforGeeks

28 Dec, 2018
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
// 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);    }}
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
// 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);    }}
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
// 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);    }}
Before removing element from TreeSet: []
First lowest element removed is : null
After removing element from TreeSet: []

java-treeset
java-treeset-functions
Picked
Java
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Constructors in Java
Stream In Java
Exceptions in Java
Different ways of Reading a text file in Java
Functional Interfaces in Java
Java Programming Examples
StringBuilder Class in Java with Examples
Checked vs Unchecked Exceptions in Java
Comparator Interface in Java with Examples
Strings in Java