在Java中同时实现Map和List接口?

发布于 2024-10-02 20:16:03 字数 787 浏览 3 评论 0 原文

我想要一个在 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.)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

不爱素颜 2024-10-09 20:16:03

需要指出的是,错误的原因是 Map 包含了以下 remove 方法的定义:

V remove(Object key)

List 定义了:

boolean remove(Object o) 

并且,在Java中,方法不能根据其返回类型进行重载,因此它们是冲突的签名,并且不能在同一个类中实现。

It should be pointed out that the reason for the error is that Map contains the definition for the following remove method:

V remove(Object key)

While List defines:

boolean remove(Object o) 

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.

ㄖ落Θ余辉 2024-10-09 20:16:03

正如您所注意到的,您不能在同一个类上同时实现 ListMap。但对于你所需要的,这也不是必要的。您需要的是 data 可以通过 MapList 接口访问。有点像在 entrySet() 或使用 Map.values()

简而言之,您需要的是数据的 2 个视图,一个视图实现 List,另一个视图实现 Map

如果有一个占主导地位的视图(例如地图),那么您可以为地图实现提供一个方法List getAsList(),它将数据显示为列表,并由地图的数据支持。

编辑

Paulo Guedes 给出的答案应该对您有用。已经有符合您要求的 Map 实现。我的回答有点笼统,即使用多个不兼容的接口呈现相同的数据,而简单的适配器是不够的。

As you noticed you cannot implement both List and Map on the same class. But for what you need that should also not be necessary. What you need is that the data can be accessed by both a Map and a List interface. A bit like accessing Map 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 implementing Map.

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.

国际总奸 2024-10-09 20:16:03

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.

意犹 2024-10-09 20:16:03

为什么不实现自己的接口呢?

public interface HashListMap {

public boolean add(Object arg0);
public void add(int arg0, Object arg1);
public boolean addAll(Collection arg0);
public boolean addAll(int arg0, Collection arg1);
public void clear();
public boolean contains(Object arg0); 
public boolean containsAll(Collection arg0);
public Object get(int arg0);
public int indexOf(Object arg0);
public boolean isEmpty();
public Iterator iterator();
public int lastIndexOf(Object arg0);
public ListIterator listIterator();
public ListIterator listIterator(int arg0);
public boolean remove(Object arg0);
public Object remove(int arg0);
public boolean removeAll(Collection arg0);
public boolean retainAll(Collection arg0);
public Object set(int arg0, Object arg1);
public int size();
public List subList(int arg0, int arg1);
public Object[] toArray();
public Object[] toArray(Object[] arg0);
public boolean containsKey(Object arg0);
public boolean containsValue(Object arg0);
public Set entrySet();
public Object get(Object arg0);
public Set keySet();
public Object put(Object arg0, Object arg1);
public void putAll(Map arg0);
public Collection values();

}

Why don't you implements your own interface?

public interface HashListMap {

public boolean add(Object arg0);
public void add(int arg0, Object arg1);
public boolean addAll(Collection arg0);
public boolean addAll(int arg0, Collection arg1);
public void clear();
public boolean contains(Object arg0); 
public boolean containsAll(Collection arg0);
public Object get(int arg0);
public int indexOf(Object arg0);
public boolean isEmpty();
public Iterator iterator();
public int lastIndexOf(Object arg0);
public ListIterator listIterator();
public ListIterator listIterator(int arg0);
public boolean remove(Object arg0);
public Object remove(int arg0);
public boolean removeAll(Collection arg0);
public boolean retainAll(Collection arg0);
public Object set(int arg0, Object arg1);
public int size();
public List subList(int arg0, int arg1);
public Object[] toArray();
public Object[] toArray(Object[] arg0);
public boolean containsKey(Object arg0);
public boolean containsValue(Object arg0);
public Set entrySet();
public Object get(Object arg0);
public Set keySet();
public Object put(Object arg0, Object arg1);
public void putAll(Map arg0);
public Collection values();

}

凹づ凸ル 2024-10-09 20:16:03

MapList 接口包含 remove 方法的冲突定义。您不能在单个类中同时实现这两种方法,因为您不能仅使用返回类型的差异来覆盖相同的方法签名。

我想知道使用 List> 是否可以满足您的需求。

The Map and List interfaces contain conflicting definitions of a remove 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.

_畞蕅 2024-10-09 20:16:03

除了 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文