IdentityHashMap class Methods in Java

java.util.IdentityHashMap

This class implements the Map interface with a hash table, using reference-equality in place of object-equality when comparing keys (and values).


Some methods of IdentityHashMap class.


clear()This method removes all of the mappings from this map.


clone()This method returns a shallow copy of this identity hash map: the keys and values themselves are not cloned.


compute(K, BiFunction)This method attempts to compute a mapping for the specified key and its current mapped value.


computeIfAbsent(K, Function)If the specified key is not already associated with a value attempt to compute its value using the given mapping function and enter it into this map unless null.


computeIfPresent(K, BiFunction)If the value for the specified key is present and non-null, attempts to compute a new mapping given the key and its current mapped value.


containsKey(Object)This method tests whether the specified object reference is a key in this identity hashmap.


containsValue(Object)This method tests whether the specified object reference is a value in this identity hashmap.


entrySet()This method returns a Set view of the mappings contained in this map. Each element in the returned set is a reference-equality-based Map


equals(Object)This method compares the specified object with this map for equality. 


forEach(BiConsumer) This method performs the given action for each entry in this map until all entries have been processed or the action throws an exception.


get(Object)This method returns the value to which the specified key is mapped, or null if this map contains no mapping for the key.


getOrDefault(Object, V)This method returns the value to which the specified key is mapped, or defaultValue if this map contains no mapping for the key.


hashCode()This method returns the hash code value for this map.


isEmpty()This method returns true if this identity hash map contains no key-value mappings.


keySet()This method returns an identity-based set view of the keys contained in this map.


merge(K, V, BiFunction)If the specified key is not already associated with a value or is associated with null, associate it with the given non-null value. 


put(K, V)This method associates the specified value with the specified key in this identity hashmap.


putAll(Map)This method copies all of the mappings from the specified map to this map.


remove(Object)This method removes the mapping for this key from this map if present.


remove(Object, Object)This method removes the entry for the specified key only if it is currently mapped to the specified value.


replace(K, V)This method replaces the entry for the specified key only if it is currently mapped to some value.


replaceAll(BiFunction)This method replaces each entry's value with the result of invoking the given function on that entry until all entries have been processed or the function throws an exception.


size()This method returns the number of key-value mappings in this identity hash map.


toString()This method returns a string representation of this map.


values()This method returns a Collection view of the values contained in this map.


IdentityHashMap values() in Java

values(): This method is available in java.util.IdentityHashMap class of Java.

Syntax:

Collection<V> java.util.IdentityHashMap.values()

This method returns a Collection view of the values contained in this map.

Parameters: NA

Returns: a collection view of the values contained in this map.

Exceptions: NA

Approach

Java

import java.util.IdentityHashMap;

public class IdentityHashMapvalues {

    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.values());

    }

}

Output:

[1, 2]


IdentityHashMap toString() in Java

toString(): This method is available in java.util.IdentityHashMap class of Java.

Syntax:

String java.util.AbstractMap.toString()

This method returns a string representation of this map.

Parameters: NA

Returns: a string representation of this map.

Exceptions: NA

Approach

Java

import java.util.IdentityHashMap;

public class IdentityHashMaptoString {

    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.toString());
    }

}

Output:

{Java=1, Hello=2}


IdentityHashMap size() in Java

size(): This method is available in java.util.IdentityHashMap class of Java.

Syntax:

int java.util.IdentityHashMap.size()

This method returns the number of key-value mappings in this identity hash map.

Parameters: NA

Returns: the number of key-value mappings in this map.

Exceptions: NA

Approach

Java

import java.util.IdentityHashMap;

public class IdentityHashMapsize {

    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.size());
    }

}

Output:

2


IdentityHashMap replaceAll(BiFunction) in Java

replaceAll(BiFunction): This method is available in java.util.IdentityHashMap class of Java.

Syntax:

void java.util.IdentityHashMap.replaceAll(BiFunction<? super K, ? super V, ? extends V> function)

This method takes one argument. This method replaces each entry's value with the result of invoking the given function on that entry until all entries have been processed or the function throws an exception.

Parameters: One parameter is required for this method.

function: the function to apply to each entry.

Returns: NA

Exceptions: NA

Approach

Java

import java.util.IdentityHashMap;
import java.util.function.BiFunction;

public class IdentityHashMapreplaceAll {

    public static void main(String args[]) {

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

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

        BiFunction<? super String, ? super Integer,
? extends Integer> function = (a, b) ->
(Integer.parseInt(a) + b);
        identityHashMap.replaceAll(function);

        System.out.println(identityHashMap);

    }

}

Output:

{1=2, 2=4}


IdentityHashMap replace(K, V) in Java

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

Syntax:

V java.util.Map.replace(K key, V value)

This method takes two arguments. This method replaces the entry for the specified key only if it is currently mapped to some value.

Parameters: Two parameters are required for this method.

key: key with which the specified value is associated.

value: value to be associated with the specified key.

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

Throws:

1. UnsupportedOperationException - if the put operation is not supported by this map.

2. ClassCastException - if the class of the specified key or value prevents it from being stored in this map.

3. NullPointerException - if the specified key or value is null, and this map does not permit null keys or values.

4. IllegalArgumentException - if some property of the specified key or value prevents it from being stored in this map.

Approach

Java

import java.util.IdentityHashMap;

public class IdentityHashMapreplace {

    public static void main(String args[]) {

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

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

        String key = "Hello";
        Integer value = 45;
        System.out.println(identityHashMap.replace(key, value));

    }

}

Output:

2


IdentityHashMap remove(Object, Object) in Java

remove(Object, Object): This method is available in java.util.IdentityHashMap class of Java.

Syntax:

boolean java.util.Map.remove(Object key, Object value)

This method takes two arguments. This method removes the entry for the specified key only if it is currently mapped to the specified value.

Parameters: Two parameters are required for this method.

key: key with which the specified value is associated.

value: value expected to be associated with the specified key.

Returns: true if the value was removed.

Throws:

1. UnsupportedOperationException - if the remove operation is not supported by this map.

2. ClassCastException - if the key or value is of an inappropriate type for this map.

3. NullPointerException - if the specified key or value is null, and this map does not permit null keys or values.

Approach

Java

import java.util.IdentityHashMap;

public class IdentityHashMapremove2 {

    public static void main(String args[]) {

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

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

        Object key = "Java", value = 1;
        System.out.println(identityHashMap.remove(key, value));

    }

}

Output:

true


IdentityHashMap remove(Object) in Java

remove(Object): This method is available in java.util.IdentityHashMap class of Java.

Syntax:

V java.util.IdentityHashMap.remove(Object key)

This method takes one argument. This method removes the mapping for this key from this map if present.

Parameters: One parameter is required for this method.

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.

Exceptions: NA

Approach

Java

import java.util.IdentityHashMap;

public class IdentityHashMapremove {

    public static void main(String args[]) {

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

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

        Object key = "Hello";

        System.out.println(identityHashMap.remove(key));
    }

}

Output:

2


IdentityHashMap putAll(Map) in Java

putAll(Map): This method is available in java.util.IdentityHashMap class of Java.

Syntax:

void java.util.IdentityHashMap.putAll(Map<? extends K, ? extends V> m).

This method copies all of the mappings from the specified map to this map. These mappings will replace any mappings that this map had for any of the keys currently in the specified map.

Parameters: One parameter is required for this method.

m: mappings to be stored in this map.

Returns: NA

Throws:

NullPointerException - if the specified map is null

Approach 1: When no exception

Java

import java.util.IdentityHashMap;

public class IdentityHashMapputAll {

    public static void main(String args[]) {

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

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

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

        identityHashMap2.putAll(identityHashMap);

        System.out.println(identityHashMap2);

    }

}

Output:

{Java=1, Hello=2}


Approach 2: NullPointerException

Java

import java.util.IdentityHashMap;

public class IdentityHashMapputAll {

    public static void main(String args[]) {

        IdentityHashMap<String, Integer> identityHashMap = null;

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

        identityHashMap2.putAll(identityHashMap);

        System.out.println(identityHashMap2);

    }

}

Output:

Exception in thread "main" java.lang.NullPointerException: Cannot invoke "java.util.Map.size()" because "m" is null at java.base/java.util.IdentityHashMap.putAll(IdentityHashMap.java:502)


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}


IdentityHashMap merge(K, V, BiFunction) in Java

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

Syntax:

V java.util.Map.merge(K key, V value, BiFunction<? super V, ? super V, ? extends V> remappingFunction)

This method takes three arguments. If the specified key is not already associated with a value or is associated with null, associate it with the given non-null value. Otherwise, replaces the associated value with the results of the given remapping function, or removes it if the result is null.

Parameters: Three parameters are required for this method.

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

value: the non-null value to be merged with the existing value associated with the key or if no existing value or a null value is associated with the key, to be associated with the key.

remappingFunction: the remapping function to recompute a value if present.

Returns: the new value associated with the specified key, or null if no value is associated with the key.

Throws:

1. UnsupportedOperationException - if the put operation is not supported by this map.

2. ClassCastException - if the class of the specified key or value prevents it from being stored in this map.

3. IllegalArgumentException - if some property of the specified key or value prevents it from being stored in this map.

4. NullPointerException - if the specified key is null and this map does not support null keys or the value or remappingFunction is null.

Approach 1: When no exception

Java

import java.util.IdentityHashMap;
import java.util.function.BiFunction;

public class IdentityHashMapmerge {

    public static void main(String args[]) {

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

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

        String key = "Hello";
        Integer value = 2;
        BiFunction<? super Integer, ? super Integer,
? extends Integer> remappingFunction =
(a, b) -> (a + b);
        identityHashMap.merge(key, value, remappingFunction);

        System.out.println(identityHashMap);

    }

}

Output:

{Java=1, Hello=4}


Approach 2: NullPointerException 

Java

import java.util.IdentityHashMap;
import java.util.function.BiFunction;

public class IdentityHashMapmerge {

    public static void main(String args[]) {

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

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

        String key = "Hello";
        Integer value = 2;
        BiFunction<? super Integer, ? super Integer, ?
extends Integer> remappingFunction = null;
        identityHashMap.merge(key, value, remappingFunction);

        System.out.println(identityHashMap);

    }

}

Output:

Exception in thread "main" java.lang.NullPointerException at java.base/java.util.Objects.requireNonNull(Objects.java:208) at java.base/java.util.Map.merge(Map.java:1265)


IdentityHashMap keySet() in Java

keySet(): This method is available in java.util.IdentityHashMap class of Java.

Syntax:

Set<K> java.util.IdentityHashMap.keySet()

This method returns an identity-based set view of the keys contained in this map. The set is backed by the map, so changes to the map are reflected in the set, and vice-versa.

Parameters: NA

Returns: an identity-based set view of the keys contained in this map.

Exceptions: NA

Approach

Java

import java.util.IdentityHashMap;

public class IdentityHashMapkeySet {

    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.keySet());

    }

}

Output:

[Java, Hello]


IdentityHashMap isEmpty() in Java

isEmpty(): This method is available in java.util.IdentityHashMap class of Java.

Syntax:

boolean java.util.IdentityHashMap.isEmpty()

This method returns true if this identity hash map contains no key-value mappings.

Parameters: NA

Returns: true if this identity hash map contains no key-value mappings.

Exceptions: NA

Approach

Java

import java.util.IdentityHashMap;

public class IdentityHashMapisEmpty {

    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.isEmpty());

    }

}

Output:

false