Collections

Java WeakHashMap methods Code With Examples

Table of Contents

Program

import java.util.WeakHashMap;
import java.util.Map;
public class WeakHashMapExample {
    public static void main(String[] args)
    {
        Map<Integer, String> weak_hash = new WeakHashMap<Integer, String>();
        //put​(K key, V value)
        weak_hash.put(10, "welcome");
        weak_hash.put(15, "to");
        weak_hash.put(20, "candid");
        weak_hash.put(25, "java");
        weak_hash.put(30, "training");
        System.out.println("Initial Mappings are: " + weak_hash);
        
        //containsKey​(Object key)
        System.out.println("\nIs the key '20' present? " +  weak_hash.containsKey(20));
        System.out.println("\nIs the key '5' present? " +  weak_hash.containsKey(5));
        
        //size()
        System.out.println("\nThe size of the map is " + weak_hash.size());
        
        //isEmpty()
        System.out.println("\nIs the map empty? "+ weak_hash.isEmpty());  
        
        //remove​(Object key)
        String returned_value = (String)weak_hash.remove(20);
        System.out.println("\nReturned value is: " + returned_value);
        
        // clear()
        weak_hash.clear();
        System.out.println("\nFinal Map: "+ weak_hash);       
    }
}

Output

Initial Mappings are: {30=training, 15=to, 10=welcome, 25=java, 20=candid}

Is the key '20' present? true

Is the key '5' present? false

The size of the map is 5

Is the map empty? false

Returned value is: candid

Description

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 – The key whose presence in this map is to be tested

Returns:

true if there is a mapping for key; false otherwise

public int size()

Returns the number of key-value mappings in this map. This result is a snapshot, and may not reflect unprocessed entries that will be removed before next attempted access because they are no longer referenced.

Specified by:

size in interface Map<K, V>

Overrides:

size in class AbstractMap<K, V>

Returns:

the number of key-value mappings in this map

public boolean isEmpty()

Returns true if this map contains no key-value mappings. This result is a snapshot, and may not reflect unprocessed entries that will be removed before next attempted access because they are no longer referenced.

Specified by:

isEmpty in interface Map<K, V>

Overrides:

isEmpty in class AbstractMap<K, V>

Returns:

true if this map contains no key-value mappings

public V remove​(Object key)

Removes the mapping for a key from this weak hash map if it is present. More formally, if this map contains a mapping from key k to value v such that (key==null ? k==null : key.equals(k)), that mapping is removed. (The map can contain at most one such mapping.)
Returns the value to which this map previously associated the key, or null if the map contained no mapping for the key. A return value of null does not necessarily indicate that the map contained no mapping for the key; it’s also possible that the map explicitly mapped the key to null.

The map will not contain a mapping for the specified key once the call returns.

Specified by:

remove in interface Map<K, V>

Overrides:

remove in class AbstractMap<K, V>

Parameters:

key – key whose mapping is to be removed from the map

Returns:

the previous value associated with key, or null if there was no mapping for key

public void clear()

Removes all of the mappings from this map. The map will be empty after this call returns.

Specified by:

clear in interface Map<K, V>

Overrides:

clear in class AbstractMap<K, V>