Java??HashMap?????????
???????????? ???????[ 2012/10/29 10:48:13 ] ????????
???????Java??????????HashMap??????????????????????Hashmap??
????????HashMap??????????????????????????HashMap???????????????????HashMap????????????????????????HashMap???????????????????????????????javadoc.
????κ??????????????HashMap????????????????????“????Hash?????”????仰?????????????????仰???????????????????????????????
??????
??????????????/???????????????????????????????????????????????/????????????????????????????????????
?????????????????????????equal??????????????ж???÷??????????????仰????????????????????????hashcode??
???????Java??????Object?????????????hashCode()?????????????????????????е??????????????????????????hash????????????????????в????hashcode??
????Entry?????????
???map?????????????????key???????value??????????????ɡ?
???????HashMap??????????????????洢??Щ??????????HashMap??????????Entry????????????????
static class Entry<K??V> implements Map.Entry<K??V>
{
final K key;
V value;
Entry<K??V> next;
final int hash;
...//More code goes here
}
?????Entry?????????????洢????????key??final????????key??value????????????????????next??hash?????????????????????Щ????????塣
put()???????????????
????????put??????????????????б???????Entry??????????е?洢??HashMap??????????????
/**
* The table?? resized as necessary. Length MUST Always be a power of two.
*/
transient Entry[] table;
??????????put??????????
/**
* Associates the specified value with the specified key in this map.
* If the map previously contained a mapping for the key?? the old
* value is replaced.
*
* @param key key with which the specified value is to be associated
* @param value value to be associated with the specified key
* @return the previous value associated with <tt>key</tt>?? or
* <tt>null</tt> if there was no mapping for <tt>key</tt>.
* (A <tt>null</tt> return can also indicate that the map
* previously associated <tt>null</tt> with <tt>key</tt>.)
*/
public V put(K key?? V value) {
if (key == null)
return putForNullKey(value);
int hash = hash(key.hashCode());
int i = indexFor(hash?? table.length);
for (Entry<K??V> e = table[i]; e != null; e = e.next) {
Object k;
if (e.hash == hash && ((k = e.key) == key || key.equals(k))) {
V oldValue = e.value;
e.value = value;
e.recordAccess(this);
return oldValue;
}
}
modCount++;
addEntry(hash?? key?? value?? i);
return null;
}
???????????????
????????key????null?????key??null???????table[0]??λ??????null??hashcode????0???????????key??hashCode()?????????????key??hash??????hash???????????洢Entry??????????е?λ?á?JDK??????????????Щ?????д????????hashCode()????????????Щ??????????С??hash????????????????????????????????hash??????????????hashCode()???????????????????????С??
??????indexFor(hash??table??length)???????????????????entry????洢????λ?á?
???????????????????????????????????????????й??????hashCode?????????????????????洢???????λ??[????bucket]???
????LinkedList??????????Entry???????next???????????????????????е?????????????????????????????
??????????????????entry????????????????洢???????????Entry??????????洢?????hashmap????λ???????????????entry?????????д????????????????????next?????????????????entry???????????洢??entry??????????????????????
????????????????key?????????value?????????????????????????滻?????????Entry?????洢λ?ú?hashmap??????????λ???entry???????????entry????equals??????????????е????ж????????????hashCode()??equals??????????????????equals??????????????滻??
??????????HashMap????key?????
get?????????????
????????????????HashMap?д洢???????????????????????????????HashMap?в???????
????????put????????????????key????佫??λ???value??????????з???null.
??????
???·???
??????????????????
2023/3/23 14:23:39???д?ò??????????
2023/3/22 16:17:39????????????????????Щ??
2022/6/14 16:14:27??????????????????????????
2021/10/18 15:37:44???????????????
2021/9/17 15:19:29???·???????·
2021/9/14 15:42:25?????????????
2021/5/28 17:25:47??????APP??????????
2021/5/8 17:01:11