在java库中LinkedHashMap有方法
protected boolean removeEldestEntry(Map.Entry<K,V> eldest)
有描述
Returns true if this map should remove its eldest entry. ... This is useful if the map represents a cache
因此,LinkedHashMap 具有可用于创建缓存的 protected 方法。
像这样:
public final class LinkedHashMapCache<K, V> extends LinkedHashMap<K, V> {
private final int MAX_LENGTH = 1000;
@Override
protected boolean removeEldestEntry(Map.Entry<K, V> eldest) {
return size() > MAX_LENGTH;
}
}
但是我的 IDE (IntelliJ IDEA) 使用检查消息突出显示了我的代码:
Class 'LinkedHashMapCache' explicitly extends 'java.util.LinkedHashMap'
IDE 用于检查的内部名称(例如,在忽略它时使用,以及在其 XML 配置文件中)是 ClassExtendsConcreteCollection
,标题和描述是:
Class explicitly extends a Collection class
Reports any clases which extend concrete classes of type java.util.Collection or java.util.Map. Subclassing collection types is a common practice of novice object-oriented developers, but is considerably more brittle than delegating collection calls.
问题:将 LinkedHashMap 子类化并禁止检查是个好主意吗?或者我需要其他解决方案?
我真正的 removeEldestEntry 方法确实比示例中的更复杂,我不仅需要检查 map 的大小,还需要检查实体值中的一些变量。 所以,我需要自定义(但简单)缓存。
最佳答案
这个检查的文字其实并没有说明真正的危险。
扩展集合类的真正危险是例如忽略重写所有 方法。例如,在扩展 Map
具体实现时,您覆盖了 .put()
而不是 .putAll()
;没有任何东西可以保证您扩展的类将使用 .put()
作为“.putAll()
后端”,因此您应该覆盖两者。
不过,就您而言,您无需担心。
然而,检查是正确的,扩展比委托(delegate)更“脆弱”。事实上,Guava有很多专门用于“集合委托(delegate)”的类,默认委托(delegate):ForwardingMap
、ForwardingList
等;然后,您仅覆盖此“委托(delegate)人”所需的方法。
关于java - LinkedHashMap 的子类作为缓存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28041601/