Java TreeMap.floorEntry(K key) example

Java TreeMap.floorEntry(K key) example

By technicalkeeda, Views 305, Date 22-Feb-2018

In this java tutorial, You will learn how to use floorEntry(K key) method of TreeMap class.Thids method returns a key-value mapping associated with the greatest key less than or equal to the given key, or null if there is no such key.

tags Java

Returns a key-value mapping associated with the greatest key less than or equal to the given key, or null if there is no such key.

syntax

public Map.Entry<K,V> floorEntry(K key)

Parameters

key - the key.

Returns

An entry with the greatest key less than or equal to key, or null if there is no such key.

Throws

ClassCastException - If the specified key cannot be compared with the keys currently in the map.

NullPointerException - if the specified key is null and this map uses natural ordering, or its comparator does not permit null keys.

Java TreeMap floorEntry(K key) example

  • Create TreeMap instance new TreeMap < Integer, String > ();
  • Add few elements in TreeMap e.g. students.put(1, "Pooja");
  • students.floorEntry(6); It will returns a key-value mapping associated (4=Aditi) with the greatest key less than or equal to the given key (6).
package com.technicalkeeda;

import java.util.TreeMap;

public class App {
    public static void main(String args[]) {

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

        students.put(1, "Pooja");
        students.put(2, "Ajit");
        students.put(3, "Yashwant");
        students.put(4, "Aditi");

        System.out.println("floor entry:- " + students.floorEntry(6));

    }
}

output

floor entry:- 4=Aditi
Yashwant

Yashwant

Hi there! I am founder of technicalkeeda.com and programming enthusiast. My skills includes Java,J2EE, Spring Framework, Nodejs, PHP and lot more. If you have any idea that you would want me to develop? Lets connect: yashwantchavan[at][gmail.com]