本文介绍了经常使用的元数据Hashmap的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
是否有任何静态大小哈希表的实现将条目限制为最近或最常用的元数据?我不想自己跟踪这些信息。
Are there any implementations of a static size hashtable that limits the entries to either the most recently or most frequently used metadata? I would prefer not to keep track of this information myself.
我知道大多数缓存组件都会跟踪这一点,但我宁愿不介绍很多新的依赖项。
I know most caching components keep track of this, but I would rather not introduce quite a lot of new dependencies.
推荐答案
您可以使用:
You can build an LRU cache using the standard JDK library using LinkedHashMap
:
public class MyLRUCache<K, V> extends LinkedHashMap<K, V> {
private final int maxEntries;
public MyLRUCache(int maxEntries) {
// you can be a bit fancy with capacity and load factor
super(16, 0.75, true);
this.maxEntries = maxEntries;
}
@Override
protected boolean removeEldestEntry(Map.Entry<K, V> eldest) {
return size() > maxEntries;
}
}
你可能想玩 s。
You may want to play with WeakReference
s as well.
这篇关于经常使用的元数据Hashmap的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!