我想创建一个新项目,类似于Util.Map.Entry,它将包含结构键,值。
问题是我不能实例化一个Map。因为它是一个接口。
有人知道如何为Map.Entry创建一个新的通用键/值对象吗?
我想创建一个新项目,类似于Util.Map.Entry,它将包含结构键,值。
问题是我不能实例化一个Map。因为它是一个接口。
有人知道如何为Map.Entry创建一个新的通用键/值对象吗?
当前回答
pair实现了java.util.Map.Entry,也可以独立使用。
另外,正如其他人提到的,Guava的com.google.common.collect.Maps.immutableEntry(K, V)也起了作用。
我更喜欢它流畅的配对。(L, R)语法。
其他回答
我定义了一个通用的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);
}
}
有一个公共静态类AbstractMap.SimpleEntry<K,V>。不要让名称的抽象部分误导您:它实际上不是抽象类(但它的顶层AbstractMap是)。
它是一个静态嵌套类的事实意味着你不需要一个封闭的AbstractMap实例来实例化它,所以像这样的东西编译很好:
Map.Entry<String,Integer> entry =
new AbstractMap.SimpleEntry<String, Integer>("exmpleString", 42);
正如在另一个回答中提到的,Guava也有一个方便的静态工厂方法Maps。你可以使用的immutableEntry。
你说:
我不会用地图。Entry本身,因为显然它是一个只读对象,我不能实例化new instanceof
这并不完全准确。为什么你不能直接实例化它(即用new)是因为它是一个接口Map.Entry。
警告和提示
如文档中所述,AbstractMap。SimpleEntry是从1.6开始使用的,所以如果您坚持使用5.0,那么它对您是不可用的。
寻找另一个实现Map的已知类。入口,实际上可以直接到javadoc。来自Java 6版本
接口映射。条目 所有已知的实现类: AbstractMap。SimpleEntry AbstractMap。SimpleImmutableEntry
不幸的是,1.5版本没有列出任何已知的可以使用的实现类,所以您可能不得不实现自己的类。
试着地图。immutableEntry来自Guava
这具有与Java 5兼容的优点(不像AbstractMap。SimpleEntry需要Java 6。)
如果你看一下Map的文档。Entry你会发现它是一个静态接口(在Map接口中定义的接口,可以通过Map.Entry访问),它有两个实现
所有已知的实现类: AbstractMap。SimpleEntry AbstractMap。SimpleImmutableEntry
类AbstractMap。SimpleEntry提供了2个构造函数:
构造函数和描述 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());
}
}
你可以只实现Map。入口<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());