Use Cases for IdentityHashMap

Can someone tell me what are some important uses for IdentityHashMap ?

+44
java
May 08 '09 at 6:51 a.m.
source share
7 answers

The docs say:

Typical use of this class is a graph of objects that preserve the transformation topology, such as serialization or deep copying. To perform such a conversion, the program must save the node table, which keeps track of all references to objects that have already been processed. The node table should not have objects, even if they are equal. Another typical use of this class is to support proxy objects. For example, a debugging tool might want to save a proxy object for each object in a program that is being debugged.

+26
May 08 '09 at 7:09 a.m.
source share
β€” -

Whenever you want your keys not to be compared with equals , but with == you used IdentityHashMap. This can be very useful if you are doing a lot of link processing but are limited to special cases only.

+31
May 08 '09 at 7:05 a.m.
source share

In one case, when you can use IdentityHashMap, your keys are class objects. This is about 33% faster than the HashMap! It probably uses less memory.

+19
Oct 21 '09 at 4:16
source share

HashMap creates Entry objects every time you add an object that can put a lot of emphasis on the GC when you have a lot of objects. In a HashMap with 1000 objects or more, you end up using the good part of your processor by simply having GC cleanup records (in situations such as path searches or other collective snapshots created and cleared). IdentityHashMap does not have this problem, so it will be much faster.

See here: http://www.javagaming.org/index.php/topic,21395.0/topicseen.html

+16
Oct 21 '09 at 17:08
source share

This is a hands-on experience from me:

IdentityHashMap leaves much less memory space compared to HashMap for high power.

+12
Oct 15 '09 at 15:53
source share

You can also use IdentityHashMap as a general-purpose map if , you can make sure that the objects you use as keys will be equal, if and only if their links are equal.

What is the benefit? Obviously, this will be faster and use less memory than using implementations like HashMap or TreeMap .




In fact, there are many things to do when it is worth it. For example:

  • Enum s. Although there is an even better alternative for listings: EnumMap
  • Class objects. They are also comparable by reference.
  • Interned String s. Either pointing them as literals, or by calling String.intern() on them.
  • Cached Instances. Some classes provide caching of their instances. For example, quoting from javadoc Integer.valueOf(int) :

    This method will always cache values ​​ranging from -128 to 127, inclusive ...

  • Some libraries / frameworks will manage exactly one instance of ceratin types, such as Spring beans.
  • Singleton types. If you use istances of types that are built using the Singleton template, you can also be sure that one instance exists of them, and therefore the equality test will be qualified for the equality test.
  • Any other type in which you explicitly use only the same links to access the values ​​that were used to place the values ​​on the map.



To demonstrate the last point:

 Map<Object, String> m = new IdentityHashMap<>(); // Any keys, we keep their references Object[] keys = { "strkey", new Object(), new Integer(1234567) }; for (int i = 0; i < keys.length; i++) m.put(keys[i], "Key #" + i); // We query values from map by the same references: for (Object key : keys) System.out.println(key + ": " + m.get(key)); 

The output will be, as expected (because we used the same Object links to the request values ​​from the map):

 strkey: Key #0 java.lang.Object@1c29bfd: Key #1 1234567: Key #2 
+11
Oct 17 '14 at 8:40
source share

The important case is that you are dealing with reference types (as opposed to values), and you really want to get the correct result. In malicious objects, the hashCode and equals methods can be overridden, which are suitable for all kinds of harm. Unfortunately, it is not used as often as it should be. If the types of interfaces you are working with do not override hashCode and equals , you should usually go for IdentityHashMap .

+3
Dec 29 '11 at 12:57
source share



All Articles