java.util.WeakHashMap.get() Method

Advertisements


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.

Declaration

Following is the declaration for java.util.WeakHashMap.get() method.

public Object get(Object key)

Parameters

  • key--the key whose associated value is to be returned.

Return Value

The method call returns the value to which this map maps the specified key, or null if the map contains no mapping for this key.

Exception

  • NA

Example

The following example shows the usage of java.util.WeakHashMap.get() method.

package com.tutorialspoint;

import java.util.Map;
import java.util.WeakHashMap;

public class WeakHashMapDemo {
   public static void main(String[] args) { 
      Map<String, String> weakHashMap = new WeakHashMap<String, String>();
      
      // put keys and values in the Map      
      weakHashMap.put("1", "first");
      weakHashMap.put("2", "two");
      weakHashMap.put("3", "three");
      
      // check existence of value for key 2
      System.out.println("Checking value for key 2");
      System.out.println(weakHashMap.get("2"));
   }    
}

Let us compile and run the above program, this will produce the following result.

Checking value for key 2
two



Advertisements
Advertisements