WeakHashMap V get(Object key) method Example Program


Returns the value to which the specified key is mapped, or null if this map contains no mapping for the key.
More formally, if this map contains a mapping from a key k to a value v such that (key==null ? k==null : key.equals(k)), then this method returns v; otherwise it returns null. (There can be at most one such mapping.)

Program

package com.candidjava;

import java.util.Map;
import java.util.WeakHashMap;
/* 
 * @author : Mohan 
 * @description : The get(Object key) method is used to return the value to which the specified key is mapped in this weak hash map, or null if the map contains no mapping for this key.
 */
public class WeakHashMapGet {
	public static void main(String[] args) {
		Map<String, String> weakHashMap = new WeakHashMap<String, String>();

		weakHashMap.put("1", "anandh");
		weakHashMap.put("2", "hari");
		weakHashMap.put("3", "vinoth");
		weakHashMap.put("4", "kamal");
		weakHashMap.put("5", "karthic");
		weakHashMap.put("6", "mohan");

		System.out.println("Checking value for key 2 ="+weakHashMap.get("2"));
	}
}

Output

Checking value for key 2 =hari

Explanation

public V get(Object key)
Returns the value to which the specified key is mapped, or null if this map contains no mapping for the key.
More formally, if this map contains a mapping from a key k to a value v such that (key==null ? k==null : key.equals(k)), then this method returns v; otherwise it returns null. (There can be at most one such mapping.)
A return value of null does not necessarily indicate that the map contains no mapping for the key; it's also possible that the map explicitly maps the key to null. The containsKey operation may be used to distinguish these two cases.

Specified by:
get in interface Map<K,V>
Overrides:
get in class AbstractMap<K,V>
Parameters:
key - the key whose associated value is to be returned
Returns:
the value to which the specified key is mapped, or null if this map contains no mapping for the key
See Also:
put(Object, Object)


Related Post

Comments


©candidjava.com