2008-10-30 08:31 |
@stanleyxu2005
關于部分,你可以看一下Jdk中鏈表的實現。LinkedHashSet實現是通過LinkedHashMap來實現
void addEntry(int hash, K key, V value, int bucketIndex) {
createEntry(hash, key, value, bucketIndex);
// Remove eldest entry if instructed, else grow capacity if appropriate
Entry<K,V> eldest = header.after;
if (removeEldestEntry(eldest)) {
removeEntryForKey(eldest.key);
} else {
if (size >= threshold)
resize(2 * table.length); //重新申請內存空間,大小為原來的兩倍,并把原來的數據內容復制
}
}
雖然注釋上說是實現了雙向鏈表,但其最終還是以數組的方式存儲。只是在實現中體表了鏈表這樣特性功能(快速新增,刪除)。
下面是它的刪除操作,其實并沒有把實際的值刪除,而是引用不到這塊內容而以(在數據組中還存在,在下次重新申請空間時,這塊才會被清除掉)
private void remove() {
before.after = after;
after.before = before;
}
回復 更多評論