Java?е?HashMap???
???????????? ???????[ 2014/7/21 10:49:25 ] ??????????????? java
???????????????鳤???????????????????????????????趼?????????????????????????????????hashmap?????16????????鳤????16???????
????threshold?????12?????????????????0.75??????????map?д??12????????map???????仯????????洢??13???????table????????????????????2????????????????????????????????1?????map?д??16????????????????????table.length = 16?????0.75???????16??????????table.length = 32????????????16????????????????16???????32???????д????????????????????16??????????Щ???????????Щ???????????????????????λ???????
????int hash = hash(key.hashCode());
????int i = indexFor(hash?? table.length);
?????????????????????????а???table.length?????仰???λ??i??hash??table.length???????????λ??i??table.length?????????????????????16???????????????????ó?hashCode???????????????λ??i???length???С?й????????????length????????????????Щ????map.getValue(key)???????????????б?????????Щ????????Щ??????Java??hashmap?????????????????????????factor???????????????????????С??table.length?С?????????????????????????????????????£?????????????10????????????????10???????????????????????10??????????????????????????Щ??????????????????????Щ???????????????????????20??????????10?????????????????е??????????????????????Щ?????????Щ?????????????????????????????????????20???????????????????????????????10??????????????????????????????????????????????????????hash?????????????????????????????????涼?????????????????????????????????????????????????????????20????????????????10???????????????????????????????????????任???????????任?????hash?????????????????任?????????????????????0.75?????????????????????????????????????????????????????????????????????????ɡ?
??????????°??????????????????hashmap?????????????????????????????????????????????????????????????????????????????????????????????????????μ???????(?μ????????????????????????)???????£???????????????????????????????????????????????飬??????1????????????table?е?λ??????????????????hashMap???????????????hashMap??????????????????????????new hashmap???????????????????????????????????????Щ????????????
????????????JDK???????????HashMap??
????1.???????????HashMap????????16?????Entry???????16???????????????????default_factor?0.75?????16*0.75=12??key??value????????Entry??????????檔
????public HashMap() {
????this.loadFactor = DEFAULT_LOAD_FACTOR;
????threshold = (int)(DEFAULT_INITIAL_CAPACITY * DEFAULT_LOAD_FACTOR);
????table = new Entry[DEFAULT_INITIAL_CAPACITY];
????init();
????}
????2.????put??????
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; } } |
???????????ж?key??????????????????????????????????????????????HashMap??null??key?????????table??table[0]λ????????table[0]λ??????г????????????
private V putForNullKey(V value) { for (Entry<K??V> e = table[0]; e != null; e = e.next) { if (e.key == null) { V oldValue = e.value; e.value = value; e.recordAccess(this); return oldValue; } } modCount++; addEntry(0?? null?? value?? 0); return null; } |
????????????????????????????????????λ??i????????????????????????????????????????????key?????????????μ?value?滻???value?????????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; } } |
??????
???·???
??????????????????
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