IdentityHashMap put(K, V) in Java

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

Syntax:

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

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

Note: If the map previously contained a mapping for the key, the old value is replaced.

Parameters: Two parameters are required for this method.

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

value: the 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.IdentityHashMap;

public class IdentityHashMapput {

    public static void main(String args[]) {

        IdentityHashMap<String, Integer> identityHashMap =
new IdentityHashMap<String, Integer>();

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

        System.out.println(identityHashMap);

    }

}

Output:

{Java=1, Hello=2}


No comments:

Post a Comment