???????У?removeEldestEntry?ж??????????????????????????LinkedHashMap?е?removeEldestEntry(eldest)???????????false?????????????LRU?????????д??????????ж?????????£????????????????removeEntryForKey?????????key?????????HashMap?????????????????????????£?
final Entry<K??V> removeEntryForKey(Object key) {
if (size == 0) {
return null;
}
int hash = (key == null) ? 0 : hash(key);
int i = indexFor(hash?? table.length);
Entry<K??V> prev = table[i];
Entry<K??V> e = prev;
while (e != null) {
Entry<K??V> next = e.next;
Object k;
if (e.hash == hash &&
((k = e.key) == key || (key != null && key.equals(k)))) {
modCount++;
size--;
if (prev == e)
table[i] = next;
else
prev.next = next;
e.recordRemoval(this);
return e;
}
prev = e;
e = next;
}
return e;
}
????removeEntryForKey??HashMap?????????LinkedHashMap??header????????????????????LinkedHashMap???????д???????????header????????????δ??????
???????????′???????????????????HashMap?е??????????e.recordRemoval(this);???????????????HashMap??????LinkedHashMap??Entry????????????????????remove()?????е????д????????????????????????????????????
????/**
????* Removes this entry from the linked list.
????*/
????private void remove() {
????before.after = after;
????after.before = before;
????}void recordRemoval(HashMap<K??V> m) {
????remove();
????}
?????????LinkedHashMap????????????????????????????????????????????header?????????β??
?????????????????????????????????????????????????????get?????????header????????????get??????header?????????β????????????get?????
????public V get(Object key) {
????Entry<K??V> e = (Entry<K??V>)getEntry(key);
????if (e == null)
????return null;
????e.recordAccess(this);
????return e.value;
????}
???????????????е?getEntry???????HashMap??getEntry????????????????????????header?????????????e.recordAccess(this)??????′???
/**
* Removes this entry from the linked list.
*/
private void remove() {
before.after = after;
after.before = before;
}
/**
* Inserts this entry before the specified existing entry in the list.
*/
private void addBefore(Entry<K??V> existingEntry) {
after  = existingEntry;
before = existingEntry.before;
before.after = this;
after.before = this;
}
/**
* This method is invoked by the superclass whenever the value
* of a pre-existing entry is read by Map.get or modified by Map.set.
* If the enclosing Map is access-ordered?? it moves the entry
* to the end of the list; otherwise?? it does nothing.
*/
void recordAccess(HashMap<K??V> m) {
LinkedHashMap<K??V> lm = (LinkedHashMap<K??V>)m;
if (lm.accessOrder) {
lm.modCount++;
remove();
addBefore(lm.header);
}
}
??????????header???????????????????????????????header?????????β????????????LinkedHashMap??????????accessOrder?????true????????FIFO????
????????Android??LRU??
????Android???????HashMap??LinkedHashMap???????????·??Щ????????????????????????????Android????LruCache????????LinkedHashMap???????????·?????????Java?????дremoveEldestEntry???ж????????????Android?????дLruCache??sizeOf?????????????С??Android??????????С?ж????????????????е???trimToSize??????????????
????Android??sizeOf??????????1???????????ж?HashMap?е????????????????????????????????дsizeOf?????????????????С??Android??safeSizeOf?????sizeOf???????????ж??????????????safeSizeOf?????????м?????????ж???????????ж?????????????
????Java??removeEldestEntry?????????????????Ч????Java??????????????????ж???????????·??????Щ??????
????sizeOf??safeSizeOf????????????put??get???????????Java??????????????????????·??????????????????????LruCache??put????????????trimToSize??????????????????????????????????????£?
public void trimToSize(int maxSize)
{
while (true)
{
Object key;
Object value;
synchronized (this) {
if ((this.size < 0) || ((this.map.isEmpty()) && (this.size != 0))) {
throw new IllegalStateException(getClass().getName() + ".sizeOf() is reporting inconsistent results!");
}
if (size <= maxSize) {
break;
}
Map.Entry toEvict = (Map.Entry)this.map.entrySet().iterator().next();
key = toEvict.getKey();
value = toEvict.getValue();
this.map.remove(key);
this.size -= safeSizeOf(key?? value);
this.evictionCount += 1;
}
entryRemoved(true?? key?? value?? null);
}
}
?????????????????Map.Entry toEvict = (Map.Entry)this.map.entrySet().iterator().next();???д?????????????ж???????????????????????????????????????????????д?????????????????????
???????????????????????Android??LinkedHashMap??Java??LinkedHashMap???·?????????????header??????????????put??get????????????????????header.after?????????????header.before???????????ù?????????Map.Entry toEvict = (Map.Entry)this.map.entrySet().iterator().next();????????????header.after????????????????????????????????????
?????????map.entrySet()??HashMap???????????????LinkedHashMap?????д??????????????????HashMap??????????
????public Set<Entry<K?? V>> entrySet() {
????Set<Entry<K?? V>> es = entrySet;
????return (es != null) ? es : (entrySet = new EntrySet());
????}
??????????????????new???EntrySet??????????EntrySet?????HashMap?ж??壬LinkedHashMap????С?
private final class EntrySet extends AbstractSet<Entry<K?? V>> {
public Iterator<Entry<K?? V>> iterator() {
return newEntryIterator();
}
public boolean contains(Object o) {
if (!(o instanceof Entry))
return false;
Entry<??? ?> e = (Entry<??? ?>) o;
return containsMapping(e.getKey()?? e.getValue());
}
public boolean remove(Object o) {
if (!(o instanceof Entry))
return false;
Entry<??? ?> e = (Entry<??? ?>)o;
return removeMapping(e.getKey()?? e.getValue());
}
public int size() {
return size;
}
public boolean isEmpty() {
return size == 0;
}
public void clear() {
HashMap.this.clear();
}
}
Iterator<Entry<K?? V>> newEntryIterator() { return new EntryIterator(); }
?????????к??????????????Map.Entry toEvict = (Map.Entry)this.map.entrySet().iterator().next()?????????newEntryIterator().next()???????(new
EntryIterator()).next()????EntryIterator????LinkedHashMap?????ж?????
private final class EntryIterator
extends LinkedHashIterator<Map.Entry<K?? V>> {
public final Map.Entry<K?? V> next() { return nextEntry(); }
}
private abstract class LinkedHashIterator<T> implements Iterator<T> {
LinkedEntry<K?? V> next = header.nxt;
LinkedEntry<K?? V> lastReturned = null;
int expectedModCount = modCount;
public final boolean hasNext() {
return next != header;
}
final LinkedEntry<K?? V> nextEntry() {
if (modCount != expectedModCount)
throw new ConcurrentModificationException();
LinkedEntry<K?? V> e = next;
if (e == header)
throw new NoSuchElementException();
next = e.nxt;
return lastReturned = e;
}
public final void remove() {
if (modCount != expectedModCount)
throw new ConcurrentModificationException();
if (lastReturned == null)
throw new IllegalStateException();
LinkedHashMap.this.remove(lastReturned.key);
lastReturned = null;
expectedModCount = modCount;
}
}
??????????????????trimToSize?е????д?????????header.next?????????????????????????