Monday, 21 April 2014

NavigableSet : pollLast : Retrieves and Removes the Highest Element

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

import java.util.*;

class NavigableSetPollLast{
 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 pollLast");
   System.out.println("pollLast removes " +mySet1.pollLast());
   System.out.println("Elements in mySet1 are");
   System.out.println(mySet1);
  }
 }
}

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

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

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

Calling pollLast
pollLast removes 6
Elements in mySet1 are
[2, 4]

Calling pollLast
pollLast removes 4
Elements in mySet1 are
[2]

Calling pollLast
pollLast removes 2
Elements in mySet1 are
[]

Calling pollLast
pollLast removes null
Elements in mySet1 are
[]



Prevoius                                                 Next                                                 Home

No comments:

Post a Comment