TreeSet ceiling method returns the closest element greater than or equal to the given element.
1 |
public E ceiling(E e) |
Parameters:
e – the value to match
Returns:
the least element greater than or equal to e, or null if there is no such element
Throws:
ClassCastException – if the specified element cannot be compared with the elements currently in the set
NullPointerException – if the specified element is null and this set uses natural ordering, or its comparator does not permit null elements
TreeSet ceiling method Example
In the below example we will see various scenarios in which ceiling method works.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
package com.kscodes.collections.samples.treeset; import java.util.TreeSet; public class CeilingExample { public static void main(String args[]) { // Create a TreeSet and initialize it TreeSet<Integer> treeSet = new TreeSet<>(); // Add few elements to the TreeSet treeSet.add(5); treeSet.add(20); treeSet.add(25); treeSet.add(35); treeSet.add(50); // Test treeSet.ceiling() for 40 System.out.println("Ceiling value for 40 :: " + treeSet.ceiling(40)); // Test treeSet.ceiling() for 60 System.out.println("Ceiling value for 60 :: " + treeSet.ceiling(60)); // Test treeSet.ceiling() for null System.out.println("Ceiling value for null :: " + treeSet.ceiling(null)); } } |
Output
