Difference Betwixt Identityhashmap Together With Hashmap Inwards Java
IdentityHashMap inwards Java was added inwards Java 1.4 only notwithstanding it's 1 of those lesser known course of pedagogy inwards Java. The principal deviation betwixt IdentityHashMap as well as HashMap inwards Java is that IdentityHashMap is a particular implementation of Map interface which doesn't use equals() as well as hashCode() method for comparing object dissimilar other implementation of Map e.g. HashMap. Instead, IdentityHashMap uses equality operator "==" to compare keys as well as values inwards Java which makes it faster compare to HashMap as well as suitable where yous demand reference equality banking concern gibe as well as instead of logical equality. By the way, IdentityHashMap is a particular implementation of Map interface much similar EnumMap only it also violates full general contract of Map interface which mandates using equals method for comparing Object. Also, IdentityHashMap vs HashMap is a good Java question as well as accept been asked a span of times.
Even though this query is non every bit pop every bit How HashMap industrial plant inwards Java or Difference betwixt Hashtable as well as HashMap, it’s notwithstanding a skilful query to ask. In this Java tutorial, nosotros volition encounter an example of IdentityHashMap as well as explores to a greater extent than or less fundamental differences betwixt IdentityHashMap as well as HashMap inwards Java.
Even though this query is non every bit pop every bit How HashMap industrial plant inwards Java or Difference betwixt Hashtable as well as HashMap, it’s notwithstanding a skilful query to ask. In this Java tutorial, nosotros volition encounter an example of IdentityHashMap as well as explores to a greater extent than or less fundamental differences betwixt IdentityHashMap as well as HashMap inwards Java.
Difference betwixt IdentityHashMap as well as HashMap
Though both HashMap as well as IdentityHashMap implements Map interface, accept fail-fast Iterator as well as non-synchronized collections, next are to a greater extent than or less fundamental differences betwixt HashMap as well as IdentityHashMap inwards Java.
1) The principal deviation betwixt HashMap vs IdentityHashMap is that IdentityHashMap uses equality operator "==" for comparing keys as well as values within Map spell HashMap uses equals method for comparing keys as well as values.
2) Unlike HashMap, who uses hashcode to abide by bucket location, IdentityHashMap besides doesn't purpose hashCode() instead it uses System.identityHashCode(object).
3) Another fundamental deviation betwixt IdentityHashMap as well as HashMap inwards Java is Speed. Since IdentityHashMap doesn't purpose equals() its comparatively faster than HashMap for object with expensive equals() as well as hashCode().
4) One to a greater extent than deviation betwixt HashMap as well as IdentityHashMap is Immutability of the key. One of the basic requirement to safely store Objects inwards HashMap is keys needs to live on immutable, IdentityHashMap doesn't require keys to live on immutable every bit it is non relied on equals as well as hashCode.
There is besides a course of pedagogy called IdentityHashtable which is analogous to Hashtable inwards Java but it’s non business office of touchstone JDK as well as available inwards com.sun... package.
Example of IdentityHashMap inwards Java
Here is an example of IdentityHashMap inwards Java which shows the fundamental deviation betwixt HashMap as well as IdentityHashMap inwards comparing Objects. IdentityHashMap uses equality operator for comparing instead of equals method inwards Java :
import java.util.IdentityHashMap;
/**
* Java plan to present deviation betwixt HashMap as well as IdentityHashMap inwards Java
* @author Javin Paul
*/
public abstract class Testing {
public static void main(String args[]) {
IdentityHashMap<String, String> identityMap = new IdentityHashMap<String, String>();
identityMap.put("sony", "bravia");
identityMap.put(new String("sony"), "mobile");
//size of identityMap should live on 2 hither because ii strings are different objects
System.out.println("Size of IdentityHashMap: " + identityMap.size());
System.out.println("IdentityHashMap: " + identityMap);
identityMap.put("sony", "videogame");
//size of identityMap notwithstanding should live on 2 because "sony" as well as "sony" is same object
System.out.println("Size of IdentityHashMap: " + identityMap.size());
System.out.println("IdentityHashMap: " + identityMap);
}
}
Output
Size of IdentityHashMap: 2
IdentityHashMap: {sony=bravia, sony=mobile}
Size of IdentityHashMap: 2
IdentityHashMap: {sony=videogame, sony=mobile}
/**
* Java plan to present deviation betwixt HashMap as well as IdentityHashMap inwards Java
* @author Javin Paul
*/
public abstract class Testing {
public static void main(String args[]) {
IdentityHashMap<String, String> identityMap = new IdentityHashMap<String, String>();
identityMap.put("sony", "bravia");
identityMap.put(new String("sony"), "mobile");
//size of identityMap should live on 2 hither because ii strings are different objects
System.out.println("Size of IdentityHashMap: " + identityMap.size());
System.out.println("IdentityHashMap: " + identityMap);
identityMap.put("sony", "videogame");
//size of identityMap notwithstanding should live on 2 because "sony" as well as "sony" is same object
System.out.println("Size of IdentityHashMap: " + identityMap.size());
System.out.println("IdentityHashMap: " + identityMap);
}
}
Output
Size of IdentityHashMap: 2
IdentityHashMap: {sony=bravia, sony=mobile}
Size of IdentityHashMap: 2
IdentityHashMap: {sony=videogame, sony=mobile}
That’s all on the deviation betwixt IdentityHashMap as well as HashMap inwards Java. As I said IdentityHashMap violates Map interface full general contract as well as should solely live on used when reference equality makes sense. As per Javadoc, IdentityHashMap is suitable to overstep away along object reference during Serialization as well as deep re-create as well as tin besides live on used to hold every bit a proxy object.
Further Learning
Java In-Depth: Become a Complete Java Engineer
Difference betwixt EnumMap as well as HashMap inwards Java
0 Response to "Difference Betwixt Identityhashmap Together With Hashmap Inwards Java"
Post a Comment