1. Background
Today opened Identityhashmap, it is dumbfounded, this is a logic ah, my program code is as follows:
1Identityhashmap<string,string> identityhashmap=NewIdentityhashmap<string, string>();2 3 Private voidInitmap () {4Identityhashmap.put ("Zhangsan", "age:18");5Identityhashmap.put ("Lisi", "age:24");6 }7 8 Publicstring Getage (string name) {9 returnidentityhashmap.get (name);Ten } One A Public Static voidMain (string[] args) { -Identityhashmaptest test=Newidentityhashmaptest (); - Test.initmap (); the -String zhangsan=NewString ("Zhangsan"); -String lisi=NewString ("Lisi"); -System.out.println ("Zhangsan is =" +test.getage (Zhangsan)); +System.out.println ("Lisi is =" +Test.getage (Lisi)); - +}
The results of the operation are as follows:
Zhangsan is =null
Lisi is =null
Why is it so?
2. Source Code Exploration
This class implements the <tt>Map</tt> interface with a hash table, using
* Reference-equality in place of object-equality when comparing keys (and
* values). In and words, in an <tt>identityhashmap</tt>
* <tt>k1</tt> and <tt>k2</tt> is considered equal if and only if
* <tt> (K1==K2) </tt>. (In normal <tt>Map</tt> implementations (like
* <tt>HashMap</tt>) The keys <tt>k1</tt> and <tt>k2</tt> are considered equal
* If and only if <tt> (K1==null k2==null:k1.equals (K2)) </tt>.)
In other words, the usage of IDENTITYHASHMAP is that if two keys are reference equal, the data that exists in the map is returned, otherwise it is returned as NULL, not as if the value of the key in HashMap is equal
(k = p.key) = = Key | | (Key! = null && key.equals (k))
Put method:
1 Publicv put (K key, V value) {2Object k =masknull (key);3object[] Tab =table;4 intLen =tab.length;5 inti =Hash (k, Len);6 7 Object Item;8 while(item = Tab[i])! =NULL) {9 if(Item = =k) {TenV OldValue = (v) tab[i + 1]; OneTab[i + 1] =value; A returnOldValue; - } -i =Nextkeyindex (i, Len); the } - -modcount++; -Tab[i] =K; +Tab[i + 1] =value; - if(++size >=threshold) +Resize (len);//len = = 2 * Current capacity. A return NULL; at}
As can be seen from here, int i = hash (k, Len); it must be an even value, and the while loop operation used here may have performance problems.
Here the key is placed in an even number position, and value is stored in the next position of the key corresponding
When the Size>threshold, it will expand the capacity.
Get method
1 PublicV get (Object key) {2Object k =masknull (key);3object[] Tab =table;4 intLen =tab.length;5 inti =Hash (k, Len);6 while(true) {7Object item =Tab[i];8 if(Item = =k)9 return(V) Tab[i + 1];Ten if(Item = =NULL) One return NULL; Ai =Nextkeyindex (i, Len); - } -}
Find value here is also used while loop lookup, constantly go to polling to find key, know encounter empty.
It is also a comparison using a strong reference, and if it is Key1==key2, the corresponding value is returned.
3. Usage Scenarios
Now it's interesting to think about the design of this map.
For example, I want to find a name in the hukou register, Zhang San, how old is he now? The country is called Zhang San so many people, hukou registration, and did not say can not duplicate name, how do I know if this Zhang San is the Zhang San you want to find it? So this time can use Identityhashmap to store, only is guaranteed strong reference time, can find, otherwise, I will tell you, I do not know Zhang three is who
Java-map-identityhashmap