What is the NavigableSet.floor() method in Java?

What is the NavigableSet.floor() method in Java? Answer

In this shot, we will learn how to use the NavigableSet.floor() method in Java. The NavigableSet.floor() method is used to obtain the greatest element that is equal to or less than a given element in a set. It returns null if there is no such element in the set.

The NavigableSet.floor() method is present in the NavigableSet interface inside the java.util package.

Let’s understand with the help of an example. Suppose that a NavigableSet contains [1, 5, 3, 9, 10, 11, 16, 19]. Let 1111 be the element for which we need to determine the floor. So, the result of NavigableSet.floor() is 1010.

Parameter

The NavigableSet.floor() accepts one parameter, i.e., the element for which we need to determine the floor value.

Return value

It returns the greatest element that is equal to or less than the given element in the set.

Code

Let’s have a look at the code.

import java.util.NavigableSet;
import java.util.TreeSet;

class Main 
{
  public static void main(String[] args)
  {
    NavigableSet<Integer> s = new TreeSet<Integer>();

    s.add(6);
    s.add(8);
    s.add(5);
    s.add(3);
    s.add(9);
    s.add(10);
    s.add(17);

    System.out.println("Greatest element less than or equal to 11 is: " + s.floor(11));
  }
}

Explanation

  • In lines 1 and 2, we import the required packages and classes.
  • In line 4 we make a Main class.
  • In line 6, we make a main() function.
  • In line 8, we create a TreeSet of Integer type. The NavigableSet is inherited from SortedSet, which is actually inherited from TreeSet only. As SortedSet and NavigableSet are interfaces, we cannot instantiate an object of them.
  • From lines 10 to 16, we added the elements into the NavigableSet by using the add() method.
  • In line 18, we use the NavigableSet.floor() method and display the result with a message.

In this way, we can use the NavigableSet.floor() method in Java.

What is the NavigableSet.floor() method in Java? Review:

In our experience, we suggest you solve this What is the NavigableSet.floor() method in Java? and gain some new skills from Professionals completely free and we assure you will be worth it.

If you are stuck anywhere between any coding problem, just visit Queslers to get the What is the NavigableSet.floor() method in Java?

Find on Educative

Conclusion:

I hope this What is the NavigableSet.floor() method in Java? would be useful for you to learn something new from this problem. If it helped you then don’t forget to bookmark our site for more Coding Solutions.

This Problem is intended for audiences of all experiences who are interested in learning about Data Science in a business context; there are no prerequisites.

Keep Learning!

More Coding Solutions >>

LeetCode Solutions

Hacker Rank Solutions

CodeChef Solutions

Leave a Reply

Your email address will not be published.