TreeMap K higherKey(K key) Method Example Program


Returns the least key strictly greater than the given key, or null if there is no such key.

Program

package com.candidjava;

import java.util.TreeMap;

/**
 * @author: Hari Haran
 * @discription:ThehigherKey(K key) method is used to return the least key
 *                             strictly greater than the given key, or null if
 *                             there is no such key.
 */

public class TreeMapHigherKey {
	public static void main(String[] args) {
		TreeMap<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("Checking values of the map");
		System.out.println("Value is: " + tm.higherKey(3));
	}
}

Output

Checking values of the map
Value is: 4

Explanation

public K higherKey(K key)
Returns the least key strictly greater than the given key, or null if there is no such key.
Specified by:
higherKey in interface NavigableMap<K,V>
Parameters:
key - the key
Returns:
the least key greater than 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