Monday 21 April 2014

NavigableSet : ceiling

E ceiling(E e)
Return the least element greater than or equal to e, or null if there is no such element.

import java.util.*;

class NavigableSetCeiling{
 public static void main(String args[]){
  NavigableSet<Integer> mySet = new TreeSet<> ();
  
  for(int i=20; i >0; i-=2)
   mySet.add(i);
   
  System.out.println("Elements in mySet are");
  System.out.println(mySet+"\n");
  
  for(int i =1; i <= 25; i+=2){
   System.out.print("Ceiling of " + i +" is ");
   System.out.println(mySet.ceiling(i));
  }   
 }
}

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

Ceiling of 1 is 2
Ceiling of 3 is 4
Ceiling of 5 is 6
Ceiling of 7 is 8
Ceiling of 9 is 10
Ceiling of 11 is 12
Ceiling of 13 is 14
Ceiling of 15 is 16
Ceiling of 17 is 18
Ceiling of 19 is 20
Ceiling of 21 is null
Ceiling of 23 is null
Ceiling of 25 is null

1. throws NullPointerException if the specified element is null and this set does not permit null elements

import java.util.*;

class NavigableSetCeilingNullPointer{
 public static void main(String args[]){
  NavigableSet<Integer> mySet = new TreeSet<> ();
  
  for(int i=20; i >0; i-=2)
   mySet.add(i);
   
  System.out.println("Elements in mySet are");
  System.out.println(mySet+"\n");

  System.out.println("Ceiling Of null is");
  mySet.ceiling(null);
 }
}

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

Ceiling Of null is
Exception in thread "main" java.lang.NullPointerException
        at java.util.TreeMap.compare(TreeMap.java:1188)
        at java.util.TreeMap.getCeilingEntry(TreeMap.java:390)
        at java.util.TreeMap.ceilingKey(TreeMap.java:747)
        at java.util.TreeSet.ceiling(TreeSet.java:436)
        at NavigableSetCeilingNullPointer.main(NavigableSetCeilingNullPointer.ja
va:14)

2. throws ClassCastException if the specified element cannot be compared with the elements currently in the set







Prevoius                                                 Next                                                 Home

No comments:

Post a Comment