Collections

Java IdentityHashMap Code With Examples

Table of Contents

Program

import java.util.Map;
import java.util.HashMap;
public class IdentityHashMapExample {

	private static final Object str = null;

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		final HashMap<String, String> identityHashMap = new HashMap<String, String>();
        identityHashMap.put("a", "Java");
        identityHashMap.put(new String("a"), "J2EE");
        identityHashMap.put("b", "J2SE");
        identityHashMap.put(new String("b"), "Spring");
        identityHashMap.put("c", "Hibernate");
        
        //keySet()   
        for (final String str : identityHashMap.keySet()) 
       {
        //get​(Object key)
            System.out.println("Key : " + str + " and Value : " + identityHashMap.get(str));
        }
        
        //size()
        System.out.println("Size of map is : " + identityHashMap.size());
        System.out.println("Here 'a' and new String('a') are considered as separate keys");
     

        Map<String, String> ihm = new HashMap<>();
        //put​(K key, V value)
        ihm.put("ihmkey","ihmvalue"); 
        ihm.put(new String("ihmkey"),"ihmvalue1"); 
        System.out.println("Size of IdentityHashMap--"+ihm.size());
          


	}

}

Output

	Key : a and Value : J2EE
	Key : b and Value : Spring
	Key : c and Value : Hibernate
	Size of map is : 3
	Here 'a' and new String('a') are considered as separate keys
 	Size of IdentityHashMap--1 */

Description

public Set keySet()

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.
If the map is modified while an iteration over the set is in progress, the results of the iteration are undefined. The set supports element removal, which removes the corresponding mapping from the map, via the Iterator. remove, Set. remove, removeAll, retainAll, and clear methods. It does not support the add or addAll methods.

While the object returned by this method implements the Set interface,
it does not obey Set’s general contract.
Like its backing map, the set returned by this method defines element equality as reference-equality rather than object-equality.

This affects the behavior of its contains, remove, containsAll, equals, and hashCode methods. The equals method of the returned set returns true only if the specified object is a set containing exactly the same object references as the returned set. The symmetry and transitivity requirements of the Object. equals contract may be violated if the set returned by this method is compared to a normal set. However, the Object. equals contract is guaranteed to hold among sets returned by this method.

The hashCode method of the returned set returns the sum of the identity hashcodes of the elements in the set, rather than the sum of their hashcodes. This is mandated by the change in the semantics of the equals method, in order to enforce the general contract of the Object.hashCode method among sets returned by this method.

Specified by:

keySet in interface Map<K>,V>

Overrides:

keySet in class AbstractMapCollection<K>,V>

Returns:

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

public V get​(Object key)

Returns the value to which the specified key is mapped,
or null if this map contains no mapping for the key.
More formally, if this map contains a mapping from a key k to a value v such that (key == k),
then this method returns v; otherwise it returns null. (There can be at most one such mapping.)

A return value of null does not necessarily indicate that the map contains no mapping for the key; it’s also possible that the map explicitly maps the key to null. The containsKey operation may be used to distinguish these two
cases.

Specified by:

get in interface Map<K>,V>

Overrides:

get in class AbstractMapCollection<K>,V>

Overrides:

key – the key whose associated value is to be returned

Returns:

the value to which the specified key is mapped, or null if this map contains no mapping for the key

public int size()

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

Specified by:

size in interface Map<K>,V>

Overrides:

size in class AbstractMapCollection<K>,V>

Returns:

the number of key-value mappings in this map

public V put​(K key, V value)

Associates the specified value with the specified key in this identity hash map. If the map previously contained a mapping for the key, the old value is replaced.

Specified by:

put in interface Map<K>,V>

Overrides:

put in class AbstractMapCollection<K>,V>

Overrides:

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.
(A null return can also indicate that the map previously associated null with key.