Java LinkedHashMap.removeEldestEntry 有什么用?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20772869/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is the use of LinkedHashMap.removeEldestEntry?
提问by user1877246
I am aware the answer to this question is easily available on the internet. I need to know what happens if I choose not to removeEldestEntry
. Below is my code:
我知道这个问题的答案很容易在互联网上找到。我需要知道如果我选择不这样做会发生什么removeEldestEntry
。下面是我的代码:
package collection;
import java.util.*;
public class MyLinkedHashMap {
private static final int MAX_ENTRIES = 2;
public static void main(String[] args) {
LinkedHashMap lhm = new LinkedHashMap(MAX_ENTRIES, 0.75F, false) {
protected boolean removeEldestEntry(Map.Entry eldest) {
return false;
}
};
lhm.put(0, "H");
lhm.put(1, "E");
lhm.put(2, "L");
lhm.put(3, "L");
lhm.put(4, "O");
System.out.println("" + lhm);
}
}
Even though I am not allowing the removeEldestEntry
my code works fine.
So, internally what is happening?
即使我不允许removeEldestEntry
我的代码工作正常。那么,内部发生了什么?
回答by VGR
Your removeEldestEntry
method is identical to the default implementation of LinkedHashMap.removeEldestEntry
, so your LinkedHashMap will simply behave like a normal LinkedHashMap with no overridden methods, retaining whatever you values and keys put into it unless and until you explicitly remove them by calling remove, removeAll, clear, etc. The advantage of using LinkedHashMap is that the collection views (keySet()
, values()
, entrySet()
) always return Iterators that traverse the keys and/or values in the order they were added to the Map.
您的removeEldestEntry
方法与 的默认实现相同LinkedHashMap.removeEldestEntry
,因此您的 LinkedHashMap 将只是像普通的 LinkedHashMap 一样没有覆盖方法,保留您放入其中的任何值和键,除非并且直到您通过调用 remove、removeAll、clear 等明确删除它们. 使用 LinkedHashMap 的优点是集合视图 ( keySet()
, values()
, entrySet()
) 总是返回迭代器,这些迭代器按照键和/或值添加到 Map 的顺序遍历它们。
回答by salbeira
removeEldestEntry
always gets checked after an element was inserted. For example, if you override the method to always return true, the LinkedHashMap will always be empty, since after every put
or putAll
insertion, the eldest element will be removed, no matter what. The JavaDoc shows a very sensible example on how to use it:
removeEldestEntry
插入元素后总是被检查。例如,如果您重写该方法以使其始终返回 true,则 LinkedHashMap 将始终为空,因为在每次插入put
或putAll
插入后,无论如何都会删除最旧的元素。JavaDoc 展示了一个关于如何使用它的非常明智的例子:
protected boolean removeEldestEntry(Map.Entry eldest){
return size() > MAX_SIZE;
}
In an alternative way, you might only want to remove an entry if it is unimportant:
或者,您可能只想删除不重要的条目:
protected boolean removeEldestEntry(Map.Entry eldest){
if(size() > MAX_ENTRIES){
if(isImportant(eldest)){
//Handle an important entry here, like reinserting it to the back of the list
this.remove(eldest.getKey());
this.put(eldest.getKey(), eldest.getValue());
//removeEldestEntry will be called again, now with the next entry
//so the size should not exceed the MAX_ENTRIES value
//WARNING: If every element is important, this will loop indefinetly!
} else {
return true; //Element is unimportant
}
return false; //Size not reached or eldest element was already handled otherwise
}
回答by David Newcomb
Why can't people just answer the OP's simple question!
为什么人们不能回答 OP 的简单问题!
If removeEldestEntry
returns false
then no items will ever be removed from the map and it will essentially behave like a normal Map
.
如果removeEldestEntry
返回,false
则永远不会从地图中删除任何项目,并且它的行为本质上与正常Map
.
回答by Arvindh Mani
Expanding on the answer by DavidNewcomb:
I'm assuming that you are learning how to implement a cache.
我假设您正在学习如何实现缓存。
The method LinkedHashMap.removeEldestEntry
is a method very commonly used in cache data structures, where the size of the cache is limited to a certain threshold. In such cases, the removeEldestEntry
method can be set to automatically remove the oldest entry when the size exceeds the threshold (defined by the MAX_ENTRIES
attribute) - as in the example provided here.
该方法LinkedHashMap.removeEldestEntry
是缓存数据结构中非常常用的一种方法,其中缓存的大小被限制在某个阈值内。在这样的情况下,所述removeEldestEntry
方法可以被设置为当尺寸超过阈值(由定义的自动删除最旧的条目MAX_ENTRIES
属性) -如在所提供的示例这里。
On the other hand, when you override the removeEldestEntry
method this way, you are ensuring that nothing ever happenswhen the MAX_ENTRIES
threshold is exceeded. In other words, the data structure would not behave like a cache, but rather a normal map.
另一方面,当您以removeEldestEntry
这种方式覆盖该方法时,您可以确保在超过阈值时不会发生任何事情MAX_ENTRIES
。换句话说,数据结构的行为不像缓存,而是法线贴图。