Previously we have discussed about
Here we learn about IdentityHashMap In Java, but before that we must know What is Map Interface In Java ? and What is Collection Interface In Java ?
Class IdentityHashMap<K,V>
All Implemented Interfaces:
Declaration of IdentityHashMap:
public class IdentityHashMap<K,V>
extends AbstractMap<K,V>
IdentityHashMap 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).
IdentityHashMap 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.
IdentityHashMap class provides all of the optional map operations, and permits null values and the null key. This class makes no guarantees as to the order of the map; in particular, it does not guarantee that the order will remain constant over time.
This class provides constant-time performance for the basic operations (get and put).
IdentityHashMap is an example of fail-fast iterator, means if the map is structurally modified at any time after the iterator is created, in any way except through the iterator's own remove method, the iterator will throw a ConcurrentModificationException.
IdentityHashMap implementation is not synchronized. If multiple threads access an identity hash map concurrently, and at least one of the threads modifies the map structurally, it must be synchronized externally. This is best done at creation time, to prevent accidental unsynchronized access to the map:
Map m = Collections.synchronizedMap(new IdentityHashMap(...));
This class is a member of the Java Collections Framework.
Constructor Summary of IdentityHashMap:
Constructor
|
Description
|
IdentityHashMap()
|
Constructs a new, empty identity hash map with a default expected
maximum size (21).
|
IdentityHashMap(int expectedMaxSize)
|
Constructs a new, empty map with the specified expected maximum size.
|
IdentityHashMap(Map<? extends K,? extends V> m)
|
Constructs a new identity hash map containing the keys-value mappings
in the specified map.
|
Method Summary of IdentityHashMap:
Modifier and Method Name
|
Description
|
void clear()
|
Removes all of the mappings from this map.
|
Object clone()
|
Returns a shallow copy of this identity hash map: the keys and values
themselves are not cloned.
|
boolean containsKey(Object key)
|
Tests whether the specified object reference is a key in this
identity hash map.
|
boolean containsValue(Object value)
|
Tests whether the specified object reference is a value in this
identity hash map.
|
Set<Map.Entry<K,V>> entrySet()
|
Returns a Set view of the mappings contained in this map.
|
boolean equals(Object o)
|
Compares the specified object with this map for equality.
|
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.
|
int hashCode()
|
Returns the hash code value for this map.
|
boolean isEmpty()
|
Returns true if this identity hash map contains no key-value
mappings.
|
Set<K> keySet()
|
Returns an identity-based set view of the keys contained in this map.
|
V put(K key, V value)
|
Associates the specified value with the specified key in this
identity hash map.
|
void putAll(Map<? extends K,? extends V> m)
|
Copies all of the mappings from the specified map to this map.
|
V remove(Object key)
|
Removes the mapping for this key from this map if present.
|
int size()
|
Returns the number of key-value mappings in this identity hash map.
|
Collection<V> values()
|
Returns a Collection view of the values contained in this map.
|
Example of IdentityHashMap:
import java.util.IdentityHashMap;
import java.util.Iterator;
import java.util.Map;
public class IdentityHashMapClassExample {
public static void main(String[] args) {
Map<String, Integer> map = new IdentityHashMap<String,Integer>();
map.put("India", 100);
map.put("Japan", 101);
map.put("Russia", 103);
map.put("USA", 105);
map.put("UK", 108);
System.out.println("Accessing IdentityHashMap through Iterator : ");
Iterator itr = map.entrySet().iterator();
while(itr.hasNext()){
Map.Entry me = (Map.Entry)itr.next();
System.out.println(me.getKey()+"\t"+me.getValue());
}
System.out.println("\nAccessing IdentityHashMap through Lambda Expression : ");
map.forEach((k,v) -> System.out.println(k+"\t"+v));
System.out.println("\nIdentityHashMap Size() : "+map.size());
System.out.println("get(e), This method return value of specified key : "+map.get("India"));
System.out.println("isEmpty(), This method return true/false : "+map.isEmpty());
System.out.println("remove(e), This method remove specified key value pair : "+map.remove("Russia"));
System.out.println("values(), This method return all values of map : "+map.values());
System.out.println("containsKey(e), This method return true/false : "+map.containsKey("India"));
System.out.println("containsValue(e), This method return true/false : "+map.containsValue(101));
}
}
Program Output:
Accessing IdentityHashMap through Iterator :
India 100
Russia 103
UK 108
USA 105
Japan 101
Accessing IdentityHashMap through Lambda Expression :
India 100
Russia 103
UK 108
USA 105
Japan 101
IdentityHashMap Size() : 5
get(e), This method return value of specified key : 100
isEmpty(), This method return true/false : false
remove(e), This method remove specified key value pair : 103
values(), This method return all values of map : [100, 108, 105, 101]
containsKey(e), This method return true/false : true
containsValue(e), This method return true/false : true
Blog Author - Pushkar Khosla,
Software Developer by Profession with 3.0 Yrs of Experience , through this blog i'am sharing my industrial Java Knowledge to entire world. For any question or query any one can comment below or mail me at pushkar.itsitm52@gmail.com.
This blog is all about to learn Core Java ,Interview Programs and Coding tricks to polish your Java Knowledge. If you like the content of this blog please share this with your friends.
Software Developer by Profession with 3.0 Yrs of Experience , through this blog i'am sharing my industrial Java Knowledge to entire world. For any question or query any one can comment below or mail me at pushkar.itsitm52@gmail.com.
This blog is all about to learn Core Java ,Interview Programs and Coding tricks to polish your Java Knowledge. If you like the content of this blog please share this with your friends.
Java I/O Tutorial

No comments:
Post a Comment