Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

treemap
java
communitycreator

What is the TreeMap.floorKey() method in Java?

Harsh Jain

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Introduction

The TreeMap.floorKey() method is present in the TreeMap class inside the java.util package.

The TreeMap.floorKey() method is used to get the largest key which is less than or equal to the key which is given in the parameter and is present in the map at that time.

A null value is returned if such a key is not present in the TreeMap.

Syntax

The syntax of the TreeMap.floorKey() method is given below:

Key floorKey();

Parameter

The TreeMap.floorKey() method accepts one parameter:

  • Key: The key for which we need to determine the floor key value.

Return

The TreeMap.floorKey() method returns one value:

  • Key: It returns the greatest key less than or equal to the given key in the parameter present in the map currently.

Code

Let’s have a look at the code:

import java.util.*;
class Main 
{
  public static void main(String[] args)
  {
    TreeMap<Integer, String> t1 = new TreeMap<Integer, String>();

    t1.put(1, "Let's");
    t1.put(5, "see");
    t1.put(2, "TreeMap class");
    t1.put(27, "methods");
    t1.put(9, "in java.");

    System.out.println("The greatest key less than or equal to " + 
      "the given key present in the map is: " + t1.floorKey(4));

    TreeMap<String, Integer> t2 = new TreeMap<String, Integer>();

    t2.put("apple", 98);
    t2.put("banana", 5);
    t2.put("carrot", 2);
    t2.put("dog", 27);
    t2.put("elephant", 9);

    System.out.println("The greatest key less than or equal to " + 
      "the given key present in the map is: " + t2.floorKey("cat"));
  }
}
Using TreeMap.floorKey() method in Java

Explanation:

  • In line 1, we import the required package.

  • In line 2, we make a Main class.

  • In line 4, we make a main() function.

  • In line 6, we declare a TreeMap consisting of keys of type Integer and values of type String.

  • In lines 8 to 12, we insert values in the TreeMap by using the TreeMap.put() method.

  • In line 14, we use TreeMap.floorKey() method and display the greatest key less than or equal to the given key from the map currently with a message.

  • In lines 17 to 26, we define another TreeMap object such that the keys are of type String and the values are of type Integer. Now, we can see in the output that the greatest key that is less than or equal to the key cat is displayed. It means that for string-based keys, the function returns the key based on alphabetical order.

So, this is the way to use the TreeMap.floorKey() method in Java.

RELATED TAGS

treemap
java
communitycreator

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring