First, Introduction
Reference types in Java are made up of four cases, strong references, soft references, weak references, and virtual references. An introduction to these can be found in my next blog post. Http://www.cnblogs.com/plxx/p/4217178.html
Ii. Overview
Weakhashmap, and HashMap are peers in the family.
public class weakhashmap<k,v> extends abstractmap<k,v> implements Map<k,v>
For Weakhashmap---an entry in a weakhashmap would automatically be removed when it key is no longer in ordinary U Se. When key is no longer in use, it is automatically cleared. What is the status of no longer used??? when GC root is not enumerated, it is assumed that this can be referred to by GC (finalizable-->finalized-->reclaimed).
Like HashMap, Weakhashmap allows <key,value> to be null, with an initial size of 16,load factor of 0.75, not thread-safe.
In a Weakhashmap
Once the key is cleared, it can no longer be created, queried, and later entry removed.
Each key object in a weakhashmap
Value objects don't strongly refer to their own keys, either directly or indirectly,
Since that'll prevent the keys from being discarded
The key is stored in weak reference. While value is stored in the strong reference,value is not known directly or indirectly associated with the key, otherwise it will prevent the removal of key.
Third, view the source code
1 Private voidexpungestaleentries () {2 for(Object x; (x = Queue.poll ()) = =NULL; ) {3 synchronized(queue) { //Add Queue Lock control 4@SuppressWarnings ("Unchecked")5Entry<k,v> e = (entry<k,v>) x;6 inti =indexfor (E.hash, table.length);7 8Entry<k,v> prev =Table[i]; //Find the bucket number where the entity is located 9Entry<k,v> p =prev;
//Find this entity Ten while(P! =NULL) { OneEntry<k,v> next =P.next; A if(p = =e) { - if(prev = =e) -Table[i] =Next; the Else -Prev.next =Next; - //must not null out e.next; - //Stale entries May is in use by a hashiterator +E.value =NULL;//Help GC -size--; + Break; A }
//Remove the entity-unrecoverable atPrev =p; -p =Next; - } - } - } -}
Expunge-stale-entries Erase stale Entities
The method is called in the related operation of resize (), size (), Gettables (). Erase objects that are not in use.
Java--Weakhashmap