# re: Java工具類學(xué)習(xí)筆記
2008-10-30 08:31 |
@stanleyxu2005
關(guān)于部分,你可以看一下Jdk中鏈表的實(shí)現(xiàn)。LinkedHashSet實(shí)現(xiàn)是通過LinkedHashMap來實(shí)現(xiàn)
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); //重新申請(qǐng)內(nèi)存空間,大小為原來的兩倍,并把原來的數(shù)據(jù)內(nèi)容復(fù)制
}
}
雖然注釋上說是實(shí)現(xiàn)了雙向鏈表,但其最終還是以數(shù)組的方式存儲(chǔ)。只是在實(shí)現(xiàn)中體表了鏈表這樣特性功能(快速新增,刪除)。
下面是它的刪除操作,其實(shí)并沒有把實(shí)際的值刪除,而是引用不到這塊內(nèi)容而以(在數(shù)據(jù)組中還存在,在下次重新申請(qǐng)空間時(shí),這塊才會(huì)被清除掉)
private void remove() {
before.after = after;
after.before = before;
}
回復(fù) 更多評(píng)論