NavigableSet Class floor() method in Java

Java 8Object Oriented ProgrammingProgramming

The floor() method returns the greatest element less than or equal to the given element i.e. 30 here −

floor(30)

The following is an example to implement the floor method in Java −

Example

 Live Demo

import java.util.NavigableSet;
import java.util.TreeSet;
public class Demo {
   public static void main(String[] args) {
      NavigableSet<Integer> set = new TreeSet<>();
      set.add(10);
      set.add(25);
      set.add(40);
      set.add(55);
      set.add(70);
      set.add(85);
      set.add(100);
      System.out.println("Returned Value = " + set.floor(30));
   }
}

Output

Returned Value = 25

raja
Published on 11-Jan-2019 05:24:17
Advertisements