Thursday, September 4, 2025
HomeLanguagesJavaNavigableSet ceiling() method in Java

NavigableSet ceiling() method in Java

The ceiling() method of NavigableSet interface in Java 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:

E ceiling(E ele)

Where, E is the type of elements maintained by this Set container.

Parameters: This function accepts a parameter ele which refers to type of element maintained by this Set container.

Return Value: It returns the least element in this set greater than or equal to the given element, or null if there is no such element.

Below programs illustrate the ceiling() method in Java:

Program 1: NavigableSet with integer elements.




// A Java program to demonstrate
// working of NavigableSet
import java.util.NavigableSet;
import java.util.TreeSet;
  
public class GFG {
    public static void main(String[] args)
    {
        NavigableSet<Integer> ns = new TreeSet<>();
        ns.add(0);
        ns.add(1);
        ns.add(2);
        ns.add(3);
        ns.add(4);
        ns.add(5);
        ns.add(6);
  
        System.out.println("Least element greater than"
               + " or equal to 4 is: " + ns.ceiling(4));
    }
}


Output:

Least element greater than or equal to 4 is: 4

Program 2: NavigableSet with string elements.




// A Java program to demonstrate
// working of NavigableSet
import java.util.NavigableSet;
import java.util.TreeSet;
  
public class GFG {
    public static void main(String[] args)
    {
        NavigableSet<String> ns = new TreeSet<>();
        ns.add("A");
        ns.add("B");
        ns.add("C");
        ns.add("D");
        ns.add("E");
        ns.add("F");
        ns.add("G");
  
        System.out.println("Least element greater than"
              + " or equal to D is: " + ns.ceiling("D"));
    }
}


Output:

Least element greater than or equal to D is: D

Reference: https://docs.oracle.com/javase/10/docs/api/java/util/NavigableSet.html#ceiling(E)

RELATED ARTICLES

Most Popular

Dominic
32261 POSTS0 COMMENTS
Milvus
81 POSTS0 COMMENTS
Nango Kala
6626 POSTS0 COMMENTS
Nicole Veronica
11795 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11855 POSTS0 COMMENTS
Shaida Kate Naidoo
6747 POSTS0 COMMENTS
Ted Musemwa
7023 POSTS0 COMMENTS
Thapelo Manthata
6695 POSTS0 COMMENTS
Umr Jansen
6714 POSTS0 COMMENTS