Monday 21 April 2014

NavigableSet : pollFirst : Retrieves and Removes the lowest Element

E pollFirst()
Retrieves and removes the lowest element, or returns null if this set is empty.

import java.util.*;

class NavigableSetPollFirst{
 public static void main(String args[]){
  NavigableSet<Integer> mySet1 = new TreeSet<> ();
  
  /* Add Elements too mySet1 */
  for(int i=10; i>0; i-=2){
   mySet1.add(i);
  }
  
  System.out.println("Elements in mySet1 are");
  System.out.println(mySet1+"\n");
  
  for(int i=0; i < 6; i++){
   System.out.println("\nCalling pollFirst");
   System.out.println("pollFirst removes " +mySet1.pollFirst());
   System.out.println("Elements in mySet1 are");
   System.out.println(mySet1);
  }
 }
}

Output
Elements in mySet1 are
[2, 4, 6, 8, 10]

Calling pollFirst
pollFirst removes 2
Elements in mySet1 are
[4, 6, 8, 10]

Calling pollFirst
pollFirst removes 4
Elements in mySet1 are
[6, 8, 10]

Calling pollFirst
pollFirst removes 6
Elements in mySet1 are
[8, 10]

Calling pollFirst
pollFirst removes 8
Elements in mySet1 are
[10]

Calling pollFirst
pollFirst removes 10
Elements in mySet1 are
[]

Calling pollFirst
pollFirst removes null
Elements in mySet1 are
[]



Prevoius                                                 Next                                                 Home

No comments:

Post a Comment