Skip to content
Related Articles

Related Articles

TreeSet pollFirst() method in Java
  • Last Updated : 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);
    }
}
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






// 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);
    }
}
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




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

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

My Personal Notes arrow_drop_up
Recommended Articles
Page :