TreeMap boolean containsKey(Object key) Method Example Program


Returns true if this map contains a mapping for the specified key.

Program

package com.candidjava;

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

/**
 * @author: Hari Haran
 * @discription: The containsKey(Object key) method is used to return 'true' if
 *               this map contains a mapping for the specified key.
 */

public class TreeMapContainsKey {
	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("Checking key value 6");
		System.out.println("Value for key 6 exists: " + tm.containsKey(6));
	}
}

Output

Checking key value 6
Value for key 6 exists: true

Explanation

public boolean containsKey(Object key)
Returns true if this map contains a mapping for the specified key.
Specified by:
containsKey in interface Map<K,V>
Overrides:
containsKey in class AbstractMap<K,V>
Parameters:
key - key whose presence in this map is to be tested
Returns:
true if this map contains a mapping for the specified 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