TreeMap Map.Entry ceilingEntry(K key) Method Example Program


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.

Program

package com.candidjava;

import java.util.TreeMap;
import java.util.NavigableMap;

/**
 * @author: Hari Haran
 * @discription: 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.
 */

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

		NavigableMap<Integer, String> tm = new TreeMap<Integer, String>();

		tm.put(1, "Anand");
		tm.put(2, "Hari Haran");
		tm.put(8, "Kamal");
		tm.put(4, "Karthikeyan");
		tm.put(5, "Mohan");
		tm.put(6, "Vinoth");
		tm.put(7, "Hari Haran");
		;

		System.out.println("Ceiling entry for 3: " + tm.ceilingEntry(3));
		System.out.println("Ceiling entry for 5: " + tm.ceilingEntry(5));
	}
}

Output

Ceiling entry for 3: 4=Karthikeyan
Ceiling entry for 5: 5=Mohan

Explanation

public Map.Entry<K,V> ceilingEntry(K key)
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.
Specified by:
ceilingEntry in interface NavigableMap<K,V>
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


Related Post

Comments


©candidjava.com