Difference Betwixt Identityhashmap As Well As Hashmap Inward Java
IdentityHashMap inwards Java was added inwards Java 1.4 merely however it's 1 of those lesser known cast inwards Java. The top dog deviation betwixt IdentityHashMap too HashMap inwards Java is that IdentityHashMap is a especial implementation of Map interface which doesn't use equals() too hashCode() method for comparing object dissimilar other implementation of Map e.g. HashMap. Instead, IdentityHashMap uses equality operator "==" to compare keys too values inwards Java which makes it faster compare to HashMap too suitable where you lot demand reference equality cheque too instead of logical equality. By the way, IdentityHashMap is a especial implementation of Map interface much similar EnumMap merely 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 too convey been asked a couplet of times.
Even though this enquiry is non every bit pop every bit How HashMap industrial plant inwards Java or Difference betwixt Hashtable too HashMap, it’s however a proficient enquiry to ask. In this Java tutorial, nosotros volition come across an illustration of IdentityHashMap too explores about telephone substitution differences betwixt IdentityHashMap too HashMap inwards Java.
Even though this enquiry is non every bit pop every bit How HashMap industrial plant inwards Java or Difference betwixt Hashtable too HashMap, it’s however a proficient enquiry to ask. In this Java tutorial, nosotros volition come across an illustration of IdentityHashMap too explores about telephone substitution differences betwixt IdentityHashMap too HashMap inwards Java.
Difference betwixt IdentityHashMap too HashMap
Though both HashMap too IdentityHashMap implements Map interface, convey fail-fast Iterator too non-synchronized collections, next are about telephone substitution differences betwixt HashMap too IdentityHashMap inwards Java. 1) The top dog deviation betwixt HashMap vs IdentityHashMap is that IdentityHashMap uses equality operator "==" for comparing keys too values within Map field HashMap uses equals method for comparing keys too values.
2) Unlike HashMap, who uses hashcode to discovery bucket location, IdentityHashMap every bit good doesn't utilisation hashCode() instead it uses System.identityHashCode(object).
3) Another telephone substitution deviation betwixt IdentityHashMap too HashMap inwards Java is Speed. Since IdentityHashMap doesn't utilisation equals() its comparatively faster than HashMap for object with expensive equals() too hashCode().
4) One to a greater extent than deviation betwixt HashMap too IdentityHashMap is Immutability of the key. One of the basic requirement to safely store Objects inwards HashMap is keys needs to last immutable, IdentityHashMap doesn't require keys to last immutable every bit it is non relied on equals too hashCode.
There is every bit good a cast called IdentityHashtable which is analogous to Hashtable inwards Java but it’s non purpose of measure JDK too available inwards com.sun... package.
Example of IdentityHashMap inwards Java
Here is an illustration of IdentityHashMap inwards Java which shows the telephone substitution deviation betwixt HashMap too IdentityHashMap inwards comparing Objects. IdentityHashMap uses equality operator for comparing instead of equals method inwards Java :
import java.util.IdentityHashMap;
/**
* Java programme to demo deviation betwixt HashMap too 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 last 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 however should last 2 because "sony" too "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 programme to demo deviation betwixt HashMap too 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 last 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 however should last 2 because "sony" too "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 too HashMap inwards Java. As I said IdentityHashMap violates Map interface full general contract too should alone last used when reference equality makes sense. As per Javadoc, IdentityHashMap is suitable to driblet dead along object reference during Serialization too deep re-create too tin dismiss every bit good last used to keep every bit a proxy object.
Further Learning
Java In-Depth: Become a Complete Java Engineer
Difference betwixt EnumMap too HashMap inwards Java
Komentar
Posting Komentar