Wednesday, July 3, 2024
HomeLanguagesJavaGetting TreeSet Element Greater than Specified Element using Ceiling Method in Java

Getting TreeSet Element Greater than Specified Element using Ceiling Method in Java

To get TreeSet Element Greater than Specified Element using ceiling() Method in Java. The ceiling method in Java return the least element in the set greater than or equal to the given element, or null if there is no such element.

The ceiling() method of java.util.TreeSet<E> class is used to return the least element in this set greater than or equal to the given element, or null if there is no such element.

Syntax:

public E ceiling(E e)

Parameters: This method takes the value e as a parameter which is to be matched.

Return Value: This method returns the least element greater than or equal to e, or null if there is no such element.

Exception: This method throws NullPointerException if the specified element is null and this set uses natural ordering, or its comparator does not permit null elements.

set = {10,20,30,40,50}

// Least element in the set greater than or equal to the 23
Ceiling value of 23: 30

// There is no such element so it returns null
Ceiling value of 55: null

Example 1:

Java




// Java Program demonstrate how to get TreeSet Element
// Greater than Specified Element using ceiling() Method
 
import java.util.*;
 
public class GFG {
    public static void main(String[] args)
    {
 
        // New TreeSet
        TreeSet<Integer> set = new TreeSet<>();
 
        // Adding element to TreeSet
        set.add(40);
        set.add(50);
        set.add(30);
        set.add(10);
        set.add(20);
 
        // Print TreeSet
        System.out.println("TreeSet: " + set);
 
        // Print ceiling of 23
        System.out.println("Ceiling value of 23: "
                           + set.ceiling(23));
    }
}


 
 

Output

TreeSet: [10, 20, 30, 40, 50]
Ceiling value of 23: 30

 

Example 2:

 

Java




// Java Program demonstrate how to get TreeSet Element
// Greater than Specified Element using ceiling() Method
 
import java.util.*;
 
public class GFG {
    public static void main(String[] args)
    {
 
        // New TreeSet
        TreeSet<Integer> set = new TreeSet<>();
 
        // Adding element to TreeSet
        set.add(40);
        set.add(50);
        set.add(30);
        set.add(10);
        set.add(20);
 
        // Print TreeSet
        System.out.println("TreeSet: " + set);
 
        // Print ceiling of 55
        System.out.println("Ceiling value of 55: "
                           + set.ceiling(55));
    }
}


 
 

Output

TreeSet: [10, 20, 30, 40, 50]
Ceiling value of 55: null

 

Nokonwaba Nkukhwana
Experience as a skilled Java developer and proven expertise in using tools and technical developments to drive improvements throughout a entire software development life cycle. I have extensive industry and full life cycle experience in a java based environment, along with exceptional analytical, design and problem solving capabilities combined with excellent communication skills and ability to work alongside teams to define and refine new functionality. Currently working in springboot projects(microservices). Considering the fact that change is good, I am always keen to new challenges and growth to sharpen my skills.
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments