WeakHashMap
The usage of WeakHashMap and HashMap is basically similar.
the difference:
- HashMap's key retains strong references to the actual object, which means that as long as the HashMap object is not destroyed, all objects referenced by HashMap's keys will not be garbage collected, and HashMap will not automatically delete the key-value pairs corresponding to these keys;
- The WeakHashMap key only retains weak references to the actual object, which means that if the object referenced by the key of the WeakHashMap object is not referenced by other strongly referenced variables, the objects referenced by these keys may be garbage collected, and WeakHashMap may also automatically delete the key-value pairs corresponding to these keys.
- Each key object in WeakHashMap only holds a weak reference to the actual object. Therefore, when the actual object corresponding to the key is garbage collected, WeakHashMap will automatically delete the key-value pair corresponding to the key.
Notice:
If you need to use the WeakHashMap key to retain weak references to the object, do not let the object referenced by the key have any strong references, otherwise the meaning of WeakHashMap will be lost.
Example:
package ; import ; public class WeakHashMapTest { public static void main(String[] args) { WeakHashMap wak = new WeakHashMap(); // Both keys are anonymous string objects (no other references) (new String("math"), new String("excellent")); (new String("Chinese"), new String("good")); //This key is a string object cached by the system ("java", new String("good"));① (wak); //{java=good, mathematics=good, Chinese=good} //Notify the system to perform garbage collection (); (); (wak);//{java=good}} }
The result is: when the system is garbage, the first two key-value pairs of the WeakHashMap object are deleted, because when the first two key-value pairs are added, these two keys are anonymous string objects. WeakHashMap only retains weak references to them, so that these two key-value pairs will be automatically deleted during garbage collection.
The key at the ① mark in the WeakHashMap object is a direct quantity of string (the system will automatically retain a strong reference to the string object), so it will not be recycled during garbage collection.
IdentityHashMap
In IdentityHashMap, if and only if the two keys are strictly equal (key1==key2), IdentityHashMap considers that the two keys are equal; compared with ordinary HashMap, as long as key1 and key2 return true through the equals() method, and their hashCode values are equal.
Notice:
IdentityHashMap is a special Map implementation! When implementing Map interfaces, it intentionally violates the usual specifications of Map: IdentityHashMap requires that two keys are strictly equal to be considered equal. IdentityHashMap does not guarantee the order between key-value pairs, nor does it guarantee that their order remains unchanged over time.
Example:
package ; import ; public class IdentityHashMapTest { public static void main(String[] args) { IdentityHashMap idenmap = new IdentityHashMap(); (new String("Chinese"), 80); (new String("Chinese"), 89); ("java", 80); ("java", 80); (idenmap); //{Chinese=80, java=80, Chinese=89} } }
4 key-value pairs are added to the IdentityHashMap object. The keys in the first two key-value pairs are the latest created string objects. They are not equal by ==, so IdentityHashMap will treat them as 2 keys; the last two key-values are all direct string quantities, and their character sequences are exactly the same. Java uses a constant pool to manage direct string quantities, so they return true by == comparison. IdentityHashMap will consider them to be the same key, so only once can be added successfully.
EnumMap
EnumMap is a Map implementation used with enum classes, and all keys in EnumMap must be enum values for a single enum class. When creating an EnumMap, it must be displayed or implicitly specified for its enum class.
EnumMap Features:
- EnumMap is saved internally as an array, so this implementation is very compact and efficient.
- EunmMap maintains the order of key-value pairs according to the natural order of keys (i.e. the order in which enum values are defined in the enum class).
- EnumMap does not allow null as key, but allows null as value. NullpointerException will be thrown if you try to use null as key.
If you just query whether the key with a value of null, or just delete the key with a value of null, no exception will be thrown.
Different from ordinary Map, when creating an EnumMap, you must specify an enum class, so that the EnumMap and the specified enum class must be associated.
Example:
package ; import ; public class EnumMapTest { public static void main(String[] args) { EnumMap map = new EnumMap(); (, "spring"); (, "summer"); (map); //{SPRING=Spring, SUMMER=Summer} } } enum Season{ SPRING,SUMMER,FAIL,WINTER }
This is the introduction to this article about the detailed explanation of WeakHashMap, IdentityHashMap, and EnumMap in Java collections. For more related contents of WeakHashMap, EnumMap, and IdentityHashMap related to Java, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!