- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
在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/
我是一名 Java 新手,在概念化如何解决尝试创建表示某些分层数据的 LinkedHashMap 的问题时遇到问题。 这是我目前所知的。在此示例中,我有一个 LinkedHashMap,它显示了展平的
我有 LinkedHashMap 列表,如何不可分割地访问每个 LinkedHashMap。 List> listOfRecords 我想要 List abc= new listOsRecords.1
我有一个 LinkedHashMap,其中包含另一个 LinkedHashMap,如下所示: LinkedHashMap> containerMap = new LinkedHashMap>(); 我
我有 2 个 LinkedHashMap, LinkedHashMap, Color> map; LinkedHashMap, Color> totalMap; 两者是相同的,但 map 已通过使用撤
我有一个 map 列表,我需要单独使用其中的每张 map 以用于进一步的目的。这是我正在使用的代码片段 for(int i = 0; i ()); } int j=0; whi
我有一个扩展 LinkedHashMap 的类 (EntireFile)。我尝试转换: EntireFile old = (EntireFile) functionReturningLinkedHas
我有一个构建 LinkedHashMap 的方法,这样我就可以保持顺序。这是我的方法: public class MyClass { public Map buildMap() {
我是 Scala 的新手。我一直在尝试将 java LinkedHashMap 转换为 Scala 中的等效集合(LinkedHashMap?)以保留插入顺序。 尝试按照其他线程中的建议进行操作,但似
我有一个LinkedHashMap看起来像这样(真的不知道如何说明 HashMap): { "10/10/2010 10:10:10" => "SomeText1", "10/10/2019
我被包裹了LinkedHashMap>进入列表; List>> list = new ArrayList(mainCodesMap.entrySet()); 哪个mainCodeMap是 Map> 的
我有两个链接的 HashMap (key - String, value = String[]),它们在两个链接的 HashMap 中具有相同的大小和相同的键,我希望能够根据键,验证一个链接 Hash
假设我有以下数据结构: LinkedHashMap> foodFamilies = new LinkedHashMap<>(); 看起来像这样: {Fruit = [{Name = Apple,
如何对 LinkedHashMap 的 Arraylist 进行排序 前任。 ArrayList> list = new ArrayList>(); LinkedHashMap lin
LinkedHashMap lHashMap = new LinkedHashMap(); lHashMap.put("One", new Integer(1)); lHashMap.
关闭。这个问题需要details or clarity .它目前不接受答案。 想改善这个问题吗?通过 editing this post 添加详细信息并澄清问题. 8年前关闭。 Improve thi
我有一个简单的问题来找到数组 A 中的第一个唯一元素。但是,令我困扰的是使用不同方法的时间复杂度。到目前为止,我已经尝试过这两种方法。 第一种方法: LinkedHashMap> map = new
这个问题已经有答案了: How do I compare strings in Java? (23 个回答) 已关闭 4 年前。 我正在尝试将一对放入 linkedhashmap 中,但是当我放入 2
我需要用Java实现很久以前在Delphi中实现的代码,我尝试使用LinkedHashMap(在Delphi中使用TStringlist),因为我需要在插入元素时获取元素的索引,问题是是它不起作用..
在正确实现 hashCode 和 equals() 的情况下,以下代码会返回 false? myLinkedHashMap.containsKey(myLinkedHashMap.keySet().i
我让 XStream 使用此 xml 为我构建链接 HashMap : #!/masterofsoundtrack/broadcast #!/masterofsoun
我是一名优秀的程序员,十分优秀!