我想要一个在 Java 中实现 Map 和 List 接口的对象。这个想法类似于这个问题中的问题: Java Ordered Map
I想要将名称/值对添加到列表中并让列表保留序列,但也能够按名称进行查找:
foo.put("name0", "value0");
foo.put("name1", "value1");
foo.get(1); --> Map.Entry("name1", "value1")
foo.get("name0"); --> "value0"
问题是:当我创建此类时:
class Foo implements Map, List {
// add all methods here
}
我收到编译错误:
"The return type is incompatible with Map.remove(Object)"
public boolean remove(Object o) {
return false;
}
如果我不实现Map 和 List 接口,那么有很多 Java 集合方法不能用于此数据结构。
(另外,上面Java Ordered Map中提出的解决方案不起作用的原因是LinkedHashMap没有get(int)方法。无法通过索引选择条目。)
I'd like to have an object that implements both the Map and the List interfaces in Java. The idea is similar to the problem in this question: Java Ordered Map
I want to add name/value pairs to a list and have the list preserve the sequence, but also be able to do lookups by name:
foo.put("name0", "value0");
foo.put("name1", "value1");
foo.get(1); --> Map.Entry("name1", "value1")
foo.get("name0"); --> "value0"
Here's the problem: when I create this class:
class Foo implements Map, List {
// add all methods here
}
I get a compile error:
"The return type is incompatible with Map.remove(Object)"
public boolean remove(Object o) {
return false;
}
If I don't implement the Map and List interfaces, then there are lots of Java collections methods that aren't available to use on this data structure.
(Also, the reason that the solution proposed in Java Ordered Map above doesn't work is that LinkedHashMap doesn't have a get(int) method. Can't select entries by index.)
发布评论
评论(6)
需要指出的是,错误的原因是
Map
包含了以下remove
方法的定义:而
List
定义了:并且,在Java中,方法不能根据其返回类型进行重载,因此它们是冲突的签名,并且不能在同一个类中实现。
It should be pointed out that the reason for the error is that
Map
contains the definition for the followingremove
method:While
List
defines:And, in Java, methods cannot be overloaded based on their return type, so they are conflicting signatures, and cannot be implemented in the same class.
正如您所注意到的,您不能在同一个类上同时实现
List
和Map
。但对于你所需要的,这也不是必要的。您需要的是data
可以通过Map
和List
接口访问。有点像在 entrySet() 或使用 Map.values()。简而言之,您需要的是数据的 2 个视图,一个视图实现
List
,另一个视图实现Map
。如果有一个占主导地位的视图(例如地图),那么您可以为地图实现提供一个方法
List getAsList()
,它将数据显示为列表,并由地图的数据支持。编辑
Paulo Guedes 给出的答案应该对您有用。已经有符合您要求的 Map 实现。我的回答有点笼统,即使用多个不兼容的接口呈现相同的数据,而简单的适配器是不够的。
As you noticed you cannot implement both
List
andMap
on the same class. But for what you need that should also not be necessary. What you need is that thedata
can be accessed by both aMap
and aList
interface. A bit like accessingMap
data as a set in entrySet() or as a Collection using Map.values().In short, what you need is 2 views on the data, one view implementing a
List
and another view implementingMap
.If there is one view dominant (for example Map) then you could give your map implementation a method
List getAsList()
which presents the data as a List, backed by the data of the Map.EDIT
The answer given by Paulo Guedes should serve you. There already is a Map implementation with your requirements. My answer is a bit more general, about presenting the same data using multiple incompatible interfaces where a simple Adapter is not enough.
LinkedHashMap 可以满足您的需要。
Map 接口的哈希表和链表实现,具有可预测的迭代顺序。此实现与 HashMap 的不同之处在于,它维护一个贯穿其所有条目的双向链表。
LinkedHashMap does what you need.
Hash table and linked list implementation of the Map interface, with predictable iteration order. This implementation differs from HashMap in that it maintains a doubly-linked list running through all of its entries.
为什么不实现自己的接口呢?
}
Why don't you implements your own interface?
}
Map
和List
接口包含remove
方法的冲突定义。您不能在单个类中同时实现这两种方法,因为您不能仅使用返回类型的差异来覆盖相同的方法签名。我想知道使用
List>
是否可以满足您的需求。The
Map
andList
interfaces contain conflicting definitions of aremove
method. You can't implement both in a single class because you cannot override the same method signature with a difference in return type only.I wonder if using
List<Map.Entry<K,V>>
would fulfill your need.除了 Dave Costa 所说的之外,您还应该使用 LinkedHashMap。这是 Map,但它保留元素插入的顺序。
作为map,它实现了values()方法,所以你可以说
new ArrayList(map.values()).get(0)
模仿列表功能。
但你也可以说
地图.get(“一”)
因为它只是一个地图实现。
In addition to what Dave Costa said you should use LinkedHashMap. This is Map but it preserves order of elements insertion.
As map it implements values() method, so you can say
new ArrayList(map.values()).get(0)
to mimic list functionality.
but you also can say
map.get("one")
because it is just a map implementation.