java.util.WeakHashMap.containsKey() Method

Advertisements


Description

The containsKey(Object key) method is used to return true if this map contains a mapping for the specified key.

Declaration

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

public boolean containsKey(Object key)

Parameters

  • key--The key whose presence in this map is to be tested.

Return Value

The method call returns true if there is a mapping for key otherwise returns false.

Exception

  • NA

Example

The following example shows the usage of java.util.WeakHashMap.containsKey() 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 key value 
      System.out.println("Checking value for key 1");
      System.out.println(weakHashMap.containsKey("1"));
   }    
} 

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

Checking value for key 1
true



Advertisements
Advertisements