关于字典:Java-如何创建新的Entry(键,值)

Java - How to create new Entry (key, value)

我想创建与Util.Map.Entry类似的新项目,其中将包含结构keyvalue

问题是我无法实例化Map.Entry,因为它是接口。

有谁知道如何为Map.Entry创建新的通用键/值对象?


public static class AbstractMap.SimpleEntry。不要让名称的Abstract部分误导您:实际上,它不是Abstract类(但它的顶级AbstractMap是)。

它是static嵌套类的事实,意味着您不需要封闭的AbstractMap实例来实例化它,因此可以进行如下编译:

1
2
Map.Entry<String,Integer> entry =
    new AbstractMap.SimpleEntry<String, Integer>("exmpleString", 42);

如另一个答案所述,番石榴还具有方便使用的static工厂方法Maps.immutableEntry

你说:

I can't use Map.Entry itself because apparently it's a read-only object that I can't instantiate new instanceof

这并不完全正确。您无法直接实例化它(即使用new)的原因是因为它是interface Map.Entry

注意和提示

如文档中所述,AbstractMap.SimpleEntry@since 1.6,因此如果您坚持使用5.0,则无法使用它。

要查找implements Map.Entry的另一个已知类,实际上您可以直接转到javadoc。从Java 6版本开始

Interface Map.Entry

All Known Implementing Classes:

  • AbstractMap.SimpleEntry, AbstractMap.SimpleImmutableEntry

不幸的是1.5版本没有列出您可以使用的任何已知实现类,因此您可能会陷入自己实现的困境。


您可以自己实现Map.Entry接口:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import java.util.Map;

final class MyEntry<K, V> implements Map.Entry<K, V> {
    private final K key;
    private V value;

    public MyEntry(K key, V value) {
        this.key = key;
        this.value = value;
    }

    @Override
    public K getKey() {
        return key;
    }

    @Override
    public V getValue() {
        return value;
    }

    @Override
    public V setValue(V value) {
        V old = this.value;
        this.value = value;
        return old;
    }
}

然后使用它:

1
2
3
Map.Entry<String, Object> entry = new MyEntry<String, Object>("Hello", 123);
System.out.println(entry.getKey());
System.out.println(entry.getValue());


从Guava尝试Maps.immutableEntry

这具有与Java 5兼容的优势(不同于AbstractMap.SimpleEntry需要Java 6)。


AbstractMap.SimpleEntry的示例:

1
2
3
import java.util.Map;
import java.util.AbstractMap;
import java.util.AbstractMap.SimpleEntry;

实例:

1
2
ArrayList<Map.Entry<Integer, Integer>> arr =
    new ArrayList<Map.Entry<Integer, Integer>>();

添加行:

1
2
3
arr.add(new AbstractMap.SimpleEntry(2, 3));
arr.add(new AbstractMap.SimpleEntry(20, 30));
arr.add(new AbstractMap.SimpleEntry(2, 4));

获取行:

1
2
3
4
5
6
System.out.println(arr.get(0).getKey());
System.out.println(arr.get(0).getValue());
System.out.println(arr.get(1).getKey());
System.out.println(arr.get(1).getValue());
System.out.println(arr.get(2).getKey());
System.out.println(arr.get(2).getValue());

应打印:

1
2
3
4
5
6
2
3
20
30
2
4

定义图结构的边缘非常有用。就像大脑中神经元之间的神经元一样。


从Java 9开始,有一个新的实用程序方法允许创建一个不可变条目Map#entry(Object, Object)

这是一个简单的示例:

1
Entry<String, String> entry = Map.entry("foo","bar");

由于它是不可变的,因此调用setValue将引发UnsupportedOperationException。其他限制是不可序列化的事实,并且禁止将null作为键或值使用,如果您不接受,则需要使用AbstractMap.SimpleImmutableEntryAbstractMap.SimpleEntry

注意:如果您需要直接创建一个0到最多10个(键,值)对的Map,则可以使用Map.of(K key1, V value1, ...)类型的方法。


为什么Map.Entry?我猜想像键值对这样的东西适合这种情况。

使用java.util.AbstractMap.SimpleImmutableEntryjava.util.AbstractMap.SimpleEntry


您实际上可以选择:
Map.Entry en= Maps.immutableEntry(key, value);


org.apache.commons.lang3.tuple.Pair实现java.util.Map.Entry,也可以独立使用。

就像其他人提到的那样,番石榴的com.google.common.collect.Maps.immutableEntry(K, V)也能解决问题。

我更喜欢Pair的流畅的Pair.of(L, R)语法。


我定义了一个我一直使用的通用Pair类。这很棒。另外,通过定义静态工厂方法(Pair.create),我只需要经常写一次类型参数。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
public class Pair<A, B> {

    private A component1;
    private B component2;

    public Pair() {
            super();
    }

    public Pair(A component1, B component2) {
            this.component1 = component1;
            this.component2 = component2;
    }

    public A fst() {
            return component1;
    }

    public void setComponent1(A component1) {
            this.component1 = component1;
    }

    public B snd() {
            return component2;
    }

    public void setComponent2(B component2) {
            this.component2 = component2;
    }

    @Override
    public String toString() {
            return"<" + component1 +"," + component2 +">";
    }

    @Override
    public int hashCode() {
            final int prime = 31;
            int result = 1;
            result = prime * result
                            + ((component1 == null) ? 0 : component1.hashCode());
            result = prime * result
                            + ((component2 == null) ? 0 : component2.hashCode());
            return result;
    }

    @Override
    public boolean equals(Object obj) {
            if (this == obj)
                    return true;
            if (obj == null)
                    return false;
            if (getClass() != obj.getClass())
                    return false;
            final Pair<?, ?> other = (Pair<?, ?>) obj;
            if (component1 == null) {
                    if (other.component1 != null)
                            return false;
            } else if (!component1.equals(other.component1))
                    return false;
            if (component2 == null) {
                    if (other.component2 != null)
                            return false;
            } else if (!component2.equals(other.component2))
                    return false;
            return true;
    }

    public static <A, B> Pair<A, B> create(A component1, B component2) {
            return new Pair<A, B>(component1, component2);
    }

}


如果您正在使用Clojure,则还有另一种选择:

1
2
3
(defn map-entry
  [k v]
  (clojure.lang.MapEntry/create k v))