我正在寻找Java中的KeyValuePair类。 因为java。util大量使用接口,没有提供具体的实现,只有Map。输入接口。
是否有可以导入的规范实现? 这是我讨厌实现100次的“水管工编程”类之一。
我正在寻找Java中的KeyValuePair类。 因为java。util大量使用接口,没有提供具体的实现,只有Map。输入接口。
是否有可以导入的规范实现? 这是我讨厌实现100次的“水管工编程”类之一。
当前回答
Commons Lang中的Pair类可能会有所帮助:
Pair<String, String> keyValue = new ImmutablePair("key", "value");
当然,您需要包括commons-lang。
其他回答
我已经在GlobalMentor的核心库中发布了一个NameValuePair类,在Maven中可用。这是一个历史悠久的正在进行的项目,因此请提交任何更改或改进的请求。
使用javafx.util.Pair对于任何两种可以实例化的类型的大多数简单键-值对已经足够了。
Pair<Integer, String> myPair = new Pair<>(7, "Seven");
Integer key = myPair.getKey();
String value = myPair.getValue();
我最喜欢的是
HashMap<Type1, Type2>
您所要做的就是为Type1指定键的数据类型,为Type2指定值的数据类型。这是我在Java中见过的最常见的键值对象。
https://docs.oracle.com/javase/7/docs/api/java/util/HashMap.html
import java.util.Map;
public class KeyValue<K, V> implements Map.Entry<K, V>
{
private K key;
private V value;
public KeyValue(K key, V value)
{
this.key = key;
this.value = value;
}
public K getKey()
{
return this.key;
}
public V getValue()
{
return this.value;
}
public K setKey(K key)
{
return this.key = key;
}
public V setValue(V value)
{
return this.value = value;
}
}
Commons Lang中的Pair类可能会有所帮助:
Pair<String, String> keyValue = new ImmutablePair("key", "value");
当然,您需要包括commons-lang。