【发布时间】:2011-03-07 20:03:18
【问题描述】:
我想创建类似于Util.Map.Entry 的新项目,它将包含key、value 结构。
问题是我无法实例化Map.Entry,因为它是一个接口。
有谁知道如何为 Map.Entry 创建一个新的通用键/值对象?
【问题讨论】:
标签: java dictionary collections key-value
我想创建类似于Util.Map.Entry 的新项目,它将包含key、value 结构。
问题是我无法实例化Map.Entry,因为它是一个接口。
有谁知道如何为 Map.Entry 创建一个新的通用键/值对象?
【问题讨论】:
标签: java dictionary collections key-value
有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。来自the Java 6 version
接口 Map.Entry
>所有已知的实现类:
很遗憾,1.5 version 没有列出您可以使用的任何已知实现类,因此您可能一直无法实现自己的类。
【讨论】:
AbstractMap.SimpleEntry 直到 Java 6 才公开,正如您在文档中看到的那样。
AbstractMap.SimpleEntry。我猜你每天都会学到一些新东西!
这具有与 Java 5 兼容的优势(与需要 Java 6 的 AbstractMap.SimpleEntry 不同。)
【讨论】:
你可以自己实现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);
}
}
【讨论】:
Pair 实现是一件坏事。 Source
org.apache.commons.lang3.tuple.Pair 实现了java.util.Map.Entry,也可以单独使用。
正如其他人提到的,Guava 的 com.google.common.collect.Maps.immutableEntry(K, V) 可以解决问题。
我更喜欢Pair,因为它流畅的Pair.of(L, R) 语法。
【讨论】:
ImmutablePair吗?
为什么是Map.Entry?我想像键值对这样的东西适合这种情况。
使用java.util.AbstractMap.SimpleImmutableEntry 或java.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 作为 key 或 value 是被禁止的,如果你不能接受,你需要使用 AbstractMap.SimpleImmutableEntry 或 AbstractMap.SimpleEntry 来代替。
注意:如果您需要直接创建具有 0 到最多 10 个(键、值)对的 Map,则可以改用 Map.of(K key1, V value1, ...) 类型的方法。
【讨论】:
Map.of("key1", "value1", "key2", "value2") 向地图添加条目。或者,如果您有现有地图:map.put(entry.getKey(), entry.getValue())
您实际上可以选择:
Map.Entry<String, String> en= Maps.immutableEntry(key, value);
【讨论】:
如果你查看 Map.Entry 的documentation 你会发现它是一个静态接口(在 Map 接口内部定义的接口,可以通过 Map.Entry 访问),它有两个实现
所有已知的实现类:
AbstractMap.SimpleEntry、AbstractMap.SimpleImmutableEntry
AbstractMap.SimpleEntry 类提供了 2 个构造函数:
构造函数和描述
AbstractMap.SimpleEntry(K key, V value)
创建一个表示从指定键到
的映射的条目 指定值。
AbstractMap.SimpleEntry(Map.Entry extends K,? extends V> entry)
创建一个表示与指定条目相同的映射的条目。
一个示例用例:
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());
}
}
【讨论】: