NavigableSet lower() method in Java

The lower() method of NavigableSet interface in Java is used to return the greatest element in this set strictly less than the given element, or null if there is no such element exists in the set.

Syntax:

E lower(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 greatest element in this set strictly less than the given element, or null if there is no such element.

Below programs illustrate the lower() method in Java:

Program 1: NavigableSet with integer elements.

filter_none

edit
close

play_arrow

link
brightness_4
code

// A Java program to demonstrate lower()
// method 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("Greatest element strictly less than"
                           + " 4 is: " + ns.lower(4));
    }
}

chevron_right


Output:

Greatest element strictly less than 4 is: 3

Program 2: NavigableSet with string elements.

filter_none

edit
close

play_arrow

link
brightness_4
code

// A Java program to lower()
// method 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("Greatest element strictly less than"
                           + " D is: " + ns.lower("D"));
    }
}

chevron_right


Output:

Greatest element strictly less than D is: C

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



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.