Java Program to Implement IdentityHashMap API

This Java program is to Implement IdentityHashMap API.This class implements the Map interface with a hash table, using reference-equality in place of object-equality when comparing keys (and values). In other words, in an IdentityHashMap, two keys k1 and k2 are considered equal if and only if (k1==k2). (In normal Map implementations (like HashMap) two keys k1 and k2 are considered equal if and only if (k1==null ? k2==null : k1.equals(k2)).)
This class is not a general-purpose Map implementation! While this class implements the Map interface, it intentionally violates Map’s general contract, which mandates the use of the equals method when comparing objects. This class is designed for use only in the rare cases wherein reference-equality semantics are required.

Here is the source code of the Java program to Implement IdentityHashMap API. The Java program is successfully compiled and run on a Linux system. The program output is also shown below.

import java.util.Collection;
import java.util.HashMap;
import java.util.IdentityHashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
 
public class IdentityHashMapImpl<K, V>
{
    private IdentityHashMap<K, V> identityHashMap;
 
    /**
     * Constructs a new, empty identity hash map with a default expected maximum
     * size (21).
     **/
    public IdentityHashMapImpl()
    {
        identityHashMap = new IdentityHashMap<K, V>();
    }
 
    /** Constructs a new, empty map with the specified expected maximum size. **/
    public IdentityHashMapImpl(int expectedMaxSize)
    {
        identityHashMap = new IdentityHashMap<>(expectedMaxSize);
    }
 
    /**
     * Constructs a new identity hash map containing the keys-value mappings in
     * the specified map.
     **/
    public IdentityHashMapImpl(Map<? extends K, ? extends V> m)
    {
        identityHashMap = new IdentityHashMap<>(m);
    }
 
    /** Removes all of the mappings from this map. **/
    public void clear()
    {
        identityHashMap.clear();
    }
 
    /** Returns true if this map contains a mapping for the specified key. **/
    public boolean containsKey(Object key) 
    {
        return identityHashMap.containsKey(key);
    }
 
    /** Returns true if this map maps one or more keys to the specified value. **/
    public boolean containsValue(Object value)
    {
        return identityHashMap.containsValue(value);
    }
 
    /** Returns a Set view of the mappings contained in this map. **/
    public Set<Map.Entry<K, V>> entrySet()
    {
        return identityHashMap.entrySet();
    }
 
    /** Returns a Set view of the keys contained in this map. **/
    public Set<K> keySet()
    {
        return identityHashMap.keySet();
    }
 
    /**
     * Returns the value to which the specified key is mapped, or null if this
     * map contains no mapping for the key.
     **/
    public V get(Object key)
    {
        return identityHashMap.get(key);
    }
 
    /** Associates the specified value with the specified key in this map. **/
    public V put(K key, V value)
    {
        return identityHashMap.put(key, value);
    }
 
    /** Copies all of the mappings from the specified map to this map. **/
    public void putAll(Map<? extends K, ? extends V> map)
    {
        identityHashMap.putAll(map);
    }
 
    /** Removes the mapping for this key from this TreeMap if present. **/
    public V remove(Object key)
    {
        return identityHashMap.remove(key);
    }
 
    /** Returns the number of key-value mappings in this map. **/
    public int size()
    {
        return identityHashMap.size();
    }
 
    /** Returns a Collection view of the values contained in this map. **/
    public Collection<V> values()
    {
        return identityHashMap.values();
    }
 
    /** Returns true if this map contains no key-value mappings. **/
    public boolean isEmpty()
    {
        return identityHashMap.isEmpty();
    }
 
    public static void main(String... arg)
    {
        IdentityHashMapImpl<Integer, Integer> identityhashMap = new IdentityHashMapImpl<Integer, Integer>();
        identityhashMap.put(1, 100);
        identityhashMap.put(2, 200);
        identityhashMap.put(3, 300);
 
        Map<Integer, Integer> anotherMap = new HashMap<Integer, Integer>();
        anotherMap.put(4, 400);
        anotherMap.put(5, 500);
 
        identityhashMap.putAll(anotherMap);
        System.out.println("the key set of the identityhashmap is ");
        Set<Integer> keySet = identityhashMap.keySet();
        Iterator<Integer> itr = keySet.iterator();
        while (itr.hasNext())
        {
            System.out.print(itr.next() + "\t");
        }
        System.out.println();
 
        System.out.println("the values of the identityhashmap is ");
        Collection<Integer> collectionValues = identityhashMap.values();
        itr = collectionValues.iterator();
        while (itr.hasNext())
        {
            System.out.print(itr.next() + "\t");
        }
        System.out.println();
 
        System.out.println("the entry set of the identityhashmap is ");
        Iterator<Entry<Integer, Integer>> eitr;
        Set<Entry<Integer, Integer>> entrySet = identityhashMap.entrySet();
        eitr = entrySet.iterator();
        while (eitr.hasNext())
        {
            System.out.println(eitr.next() + "\t");
        }
        System.out.println("the identityhashmap contains Key 3 :" + identityhashMap.containsKey(3));
        System.out.println("the identityhashmap contains Value 600 :" + identityhashMap.containsValue(600));
 
        System.out.println("the size of the identityhashmap is " + identityhashMap.size());
        identityhashMap.clear();
        if (identityhashMap.isEmpty())
            System.out.println("the identityhashmap is empty");
        else
            System.out.println("the identityhashmap is not empty");
    }
}
$ javac  IdenityHashMapImpl.java
$ java IdentityHashMapImpl
the key set of the identityhashmap is 
1	4	3	5	2	
the values of the identityhashmap is 
100	400	300	500	200	
the entry set of the identityhashmap is 
1=100	
4=400	
3=300	
5=500	
2=200	
the identityhashmap contains Key 3 :true
the identityhashmap contains Value 600 :false
the size of the identityhashmap is 5
the identityhashmap is empty

Related posts:

Java Program to Implement Direct Addressing Tables
Tạo ứng dụng Java RESTful Client với thư viện Retrofit
Java Program to find the peak element of an array using Binary Search approach
Annotation trong Java 8
Returning Image/Media Data with Spring MVC
Java Program to Generate Random Numbers Using Probability Distribution Function
New Features in Java 14
Guide to CopyOnWriteArrayList
Request a Delivery / Read Receipt in Javamail
Deploy a Spring Boot WAR into a Tomcat Server
Java Program to Implement Booth Algorithm
Mệnh đề Switch-case trong java
Java Program to Find Shortest Path Between All Vertices Using Floyd-Warshall’s Algorithm
ETags for REST with Spring
Java Program to Find the Shortest Path Between Two Vertices Using Dijkstra’s Algorithm
ClassNotFoundException vs NoClassDefFoundError
Hướng dẫn Java Design Pattern – Service Locator
Automatic Property Expansion with Spring Boot
Java Program to Perform Postorder Recursive Traversal of a Given Binary Tree
Java Program to Implement vector
Loại bỏ các phần tử trùng trong một ArrayList như thế nào?
Spring Boot Application as a Service
Java Program to Implement Hash Tables with Double Hashing
Working With Maps Using Streams
Java Program to Implement Cubic convergence 1/pi Algorithm
Spring Data Java 8 Support
Giới thiệu HATEOAS
Guide to the ConcurrentSkipListMap
Java Program to Generate All Subsets of a Given Set in the Gray Code Order
Java – Rename or Move a File
Cài đặt và sử dụng Swagger UI
Java Program to Implement Bit Array