Java TreeMap.ceilingEntry(K key) example

Java TreeMap.ceilingEntry(K key) example

By technicalkeeda, Views 286, Date 21-Feb-2018

In this java tutorial, You will learn how to use ceilingEntry(K key) method of TreeMap class.The ceilingEntry(K key) method is used to return a key-value mapping associated with the least key greater 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 least key greater than or equal to the given key, or null if there is no such key.

syntax

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

Parameters

key - the key

Returns

An entry with the least key greater 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 ceilingEntry(K key) example

  • Create TreeMap instance new TreeMap < Integer, String > ();
  • Add some few elements in TreeMap e.g. students.put(1, "Pooja");
  • Print the TreeMap value.
  • students.ceilingEntry(2); It will return an entry with the least key greater than or equal to key 2.
  • students.ceilingEntry(5); It will return an entry with the least key greater than or equal to key 5.
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");
        students.put(8, "Mahesh");

        System.out.println("TreeMap values :- " + students);

        System.out.println("Ceiling key entry for key 2:- " + students.ceilingEntry(2));
        System.out.println("Ceiling key entry for key 5:- " + students.ceilingEntry(5));

    }
}

output

TreeMap values :- {1=Pooja, 2=Ajit, 3=Yashwant, 4=Aditi, 8=Mahesh}
Ceiling key entry for key 2:- 2=Ajit
Ceiling key entry for key 5:- 8=Mahesh
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]