如何创建新的条目(键,值)

我想创建一个类似于Util.Map.Entry的新项,它将包含结构keyvalue

问题是我不能实例化Map.Entry,因为它是一个接口。

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

445441 次浏览

public static class AbstractMap.SimpleEntry<K,V>。不要让名字的Abstract部分误导你:它实际上是一个abstract类(但它的顶层AbstractMap是)。

它是一个static嵌套类的事实意味着你需要一个封闭的AbstractMap实例来实例化它,所以这样的代码编译起来很好:

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

正如在另一个回答中提到的,Guava也有一个方便的static工厂方法Maps.immutableEntry可供使用。


你说:

我不能使用Map.Entry本身,因为它显然是一个只读对象,所以我不能实例化新的instanceof

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


警告和提示

正如文档中所指出的,AbstractMap.SimpleEntry@since 1.6,所以如果你坚持使用5.0,那么它对你是不可用的。

要寻找另一个implements Map.Entry的已知类,实际上可以直接访问javadoc。从Java 6版本

接口映射。条目

所有已知的实现类:

不幸的是1.5版本没有列出任何已知的你可以使用的实现类,所以你可能不得不实现你自己的。

试试番石榴中的Maps.immutableEntry

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

你可以自己实现Map.Entry<K, V>接口:

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;
}
}

然后使用它:

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

我定义了一个通用的Pair类,我一直在使用它。太棒了。作为一个额外的好处,通过定义静态工厂方法(Pair.create),我只需要把写类型参数的次数减少一半。

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);
}


}

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

另外,正如其他人提到的,番石榴的com.google.common.collect.Maps.immutableEntry(K, V)也有这个作用。

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

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

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

AbstractMap示例。SimpleEntry:

import java.util.Map;
import java.util.AbstractMap;
import java.util.AbstractMap.SimpleEntry;

实例化:

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

添加行:

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

获取行:

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());

应该打印:

2
3
20
30
2
4

它很好地定义了图结构的边。就像你大脑中神经元之间的那些。

如果你正在使用Clojure,你还有另一个选择:

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

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

这里有一个简单的例子:

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<String, String> en= Maps.immutableEntry(key, value); < / p >

如果你看Map的文档。Entry你会发现它是一个静态接口(在Map接口中定义的接口,可以通过Map.Entry访问),它有两个实现

< p > 所有已知的实现类:
AbstractMap。SimpleEntry AbstractMap。SimpleImmutableEntry < / p >

AbstractMap。SimpleEntry提供了两个构造函数:

< p > 构造函数和描述
AbstractMap。SimpleEntry(K键,V值)
创建一个表示从指定键到
的映射的条目 指定的值。
AbstractMap.SimpleEntry (Map.Entry< ?扩展了K, ?V>延伸;条目)

.创建一个表示与指定条目相同映射的条目

一个示例用例:

import java.util.Map;
import java.util.AbstractMap.SimpleEntry;


public class MyClass {
public static void main(String args[]) {
Map.Entry e = new SimpleEntry<String, String>("Hello","World");


System.out.println(e.getKey()+" "+e.getValue());
}
}