Saturday, 26 April 2014

TreeSet : last : Get the highest Element currently in the Set

E last()
Returns the highest element currently in this set.

import java.util.*;

class TreeSetLast{
 public static void main(String args[]){
  TreeSet<Integer> mySet = new TreeSet<> ();
  
  /* Add Elements to mySet */
  for(int i=20; i>0; i-=2)
   mySet.add(i);
   
  System.out.println("Elements in mySet are");
  System.out.println(mySet);
  
  System.out.println("\nHighest Element in the Set is");
  System.out.println(mySet.last());
 }
}

Output
Elements in mySet are
[2, 4, 6, 8, 10, 12, 14, 16, 18, 20]

Highest Element in the Set is
20

Prevoius                                                 Next                                                 Home

No comments:

Post a Comment