LruCache Android .
.
import java.util.LinkedHashMap;
import java.util.Map;
public class RemoveOldHashMap<K, V> {
private final LinkedHashMap<K, V> map;
private int size;
private int maxSize;
private int putCount;
private int createCount;
private int evictionCount;
private int hitCount;
private int missCount;
public RemoveOldHashMap(int maxSize) {
if (maxSize <= 0) {
throw new IllegalArgumentException("maxSize <= 0");
}
this.maxSize = maxSize;
this.map = new LinkedHashMap<K, V>(0, 0.75f, false);
}
public synchronized final V get(K key) {
if (key == null) {
throw new NullPointerException("key == null");
}
for (K k : map.keySet()) {
System.out.println("k = " + k);
}
V result = map.get(key);
for (K k : map.keySet()) {
System.out.println("k = " + k);
}
if (result != null) {
hitCount++;
return result;
}
missCount++;
result = create(key);
if (result != null) {
createCount++;
size += safeSizeOf(key, result);
map.put(key, result);
trimToSize(maxSize);
}
return result;
}
public synchronized final V put(K key, V value) {
if (key == null || value == null) {
throw new NullPointerException("key == null || value == null");
}
putCount++;
size += safeSizeOf(key, value);
V previous = map.put(key, value);
if (previous != null) {
size -= safeSizeOf(key, previous);
}
trimToSize(maxSize);
return previous;
}
private void trimToSize(int maxSize) {
while (size > maxSize && !map.isEmpty()) {
Map.Entry<K, V> toEvict = map.entrySet().iterator().next();
if (toEvict == null) {
break;
}
K key = toEvict.getKey();
V value = toEvict.getValue();
map.remove(key);
size -= safeSizeOf(key, value);
evictionCount++;
entryEvicted(key, value);
}
if (size < 0 || (map.isEmpty() && size != 0)) {
throw new IllegalStateException(getClass().getName()
+ ".sizeOf() is reporting inconsistent results!");
}
}
public synchronized final V remove(K key) {
if (key == null) {
throw new NullPointerException("key == null");
}
V previous = map.remove(key);
if (previous != null) {
size -= safeSizeOf(key, previous);
}
return previous;
}
protected void entryEvicted(K key, V value) {}
protected V create(K key) {
return null;
}
private int safeSizeOf(K key, V value) {
int result = sizeOf(key, value);
if (result < 0) {
throw new IllegalStateException("Negative size: " + key + "=" + value);
}
return result;
}
protected int sizeOf(K key, V value) {
return 1;
}
public synchronized final void evictAll() {
trimToSize(-1);
}
public synchronized final int size() {
return size;
}
public synchronized final int maxSize() {
return maxSize;
}
public synchronized final int hitCount() {
return hitCount;
}
public synchronized final int missCount() {
return missCount;
}
public synchronized final int createCount() {
return createCount;
}
public synchronized final int putCount() {
return putCount;
}
public synchronized final int evictionCount() {
return evictionCount;
}
public synchronized final Map<K, V> snapshot() {
return new LinkedHashMap<K, V>(map);
}
@Override public synchronized final String toString() {
int accesses = hitCount + missCount;
int hitPercent = accesses != 0 ? (100 * hitCount / accesses) : 0;
return String.format("LruCache[maxSize=%d,hits=%d,misses=%d,hitRate=%d%%]",
maxSize, hitCount, missCount, hitPercent);
}
}
String Integer. - 2 , .
RemoveOldHashMap<Integer, String> hash = new RemoveOldHashMap<Integer, String>(2 ) {
@Override
protected int sizeOf(Integer key, String value) {
return 1;
}
};
: LruCache