WeakHashMap put(K, V) in Java

put(K, V): This method is available in java.util.WeakHashMap class of Java.

Syntax:

V java.util.WeakHashMap.put(K key, V value)

This method takes two arguments. This method associates the specified value with the specified key in this map.

Note: The old value is replaced if the map previously contained a mapping for this key.

Parameters: Two parameters are required for this method.

key: key with which the specified value is to be associated.

value: value to be associated with the specified key.

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

Exceptions: NA

Approach

Java

import java.util.WeakHashMap;

public class WeakHashMapput {
    public static void main(String[] args) {

        WeakHashMap<String, Integer> weakHashMap =
new WeakHashMap<String, Integer>();

        weakHashMap.put("Java", 2);
        weakHashMap.put("Hello", 1);

        System.out.println(weakHashMap);

    }
}

Output:

{Hello=1, Java=2}


No comments:

Post a Comment