Java-如何创建新条目(键、值)

Java-如何创建新条目(键、值),java,dictionary,collections,key-value,Java,Dictionary,Collections,Key Value,我想创建一个类似于Util.Map.Entry的新项目,该项目将包含结构键,值 问题是我无法实例化Map.Entry,因为它是一个接口 有人知道如何为Map.Entry创建新的通用键/值对象吗?有。不要让名称的抽象部分误导您:它实际上不是抽象类(但它的顶级是) 事实上,它是一个静态嵌套类,这意味着您不需要一个封闭的抽象映射实例来实例化它,因此类似这样的编译很好: Map.Entry<String,Integer> entry = new AbstractMap.SimpleE

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

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

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

有。不要让名称的
抽象部分误导您:它实际上不是
抽象类(但它的顶级是)

事实上,它是一个
静态
嵌套类,这意味着您不需要一个封闭的
抽象映射
实例来实例化它,因此类似这样的编译很好:

Map.Entry<String,Integer> entry =
    new AbstractMap.SimpleEntry<String, Integer>("exmpleString", 42);
Map.Entry=
新的AbstractMap.SimpleEntry(“ExampleString”,42);
如另一个答案所述,番石榴还有一个方便的
静态
工厂方法,您可以使用


你说:

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

这并不完全准确。无法直接实例化它(即使用
new
)的原因是因为它是一个


警告和提示 如文档中所述,
AbstractMap.SimpleEntry
是从1.6开始的
,因此如果您坚持使用5.0,那么它对您不可用

要查找另一个实现Map.Entry的已知类,实际上可以直接转到javadoc。从

接口映射项 所有已知的实现类

  • ,
不幸的是,没有列出您可以使用的任何已知实现类,因此您可能无法实现自己的类。


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

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

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;
    }
}
import java.util.Map;
最后一个类MyEntry实现Map.Entry{
私钥;
私人价值;
公共MyEntry(K键,V值){
this.key=key;
这个值=值;
}
@凌驾
公共K getKey(){
返回键;
}
@凌驾
public V getValue(){
返回值;
}
@凌驾
公共V设置值(V值){
V old=该值;
这个值=值;
返老还童;
}
}
然后使用它:

Map.Entry<String, Object> entry = new MyEntry<String, Object>("Hello", 123);
System.out.println(entry.getKey());
System.out.println(entry.getValue());
Map.Entry=newmyentry(“你好”,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);
    }

}
公共类对{
私有A组件1;
私有B组件2;
公共对(){
超级();
}
公共对(A组件1,B组件2){
this.component1=component1;
this.component2=component2;
}
公共A fst(){
返回组件1;
}
公共无效集合组件1(组件1){
this.component1=component1;
}
公共B snd(){
返回组件2;
}
公共无效集合组件2(B组件2){
this.component2=component2;
}
@凌驾
公共字符串toString(){
返回“”;
}
@凌驾
公共int hashCode(){
最终整数素数=31;
int结果=1;
结果=素数*结果
+((component1==null)?0:component1.hashCode());
结果=素数*结果
+((component2==null)?0:component2.hashCode();
返回结果;
}
@凌驾
公共布尔等于(对象obj){
if(this==obj)
返回true;
if(obj==null)
返回false;
如果(getClass()!=obj.getClass())
返回false;
最后一对其他=(对)obj;
如果(组件1==null){
if(other.component1!=null)
返回false;
}如果(!component1.equals(other.component1))
返回false;
如果(组件2==null){
if(other.component2!=null)
返回false;
}如果(!component2.equals(other.component2))
返回false;
返回true;
}
公共静态对创建(A组件1,B组件2){
返回新的一对(组件1、组件2);
}
}

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

正如其他人提到的那样,Guava的
com.google.common.collect.Maps.immutableEntry(K,V)
起到了作用


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

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


使用
java.util.AbstractMap.SimpleImmutableEntry
java.util.AbstractMap.SimpleEntry

抽象地图示例。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
实例化:

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
获取行:

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
应打印:

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

Java9开始,有一个新的实用程序方法,允许创建一个不可变的条目

下面是一个简单的例子:

Entry<String, String> entry = Map.entry("foo", "bar");
Entry-Entry=Map.Entry(“foo”、“bar”);
由于它是不可变的,调用
setValue
将抛出一个
不支持操作异常
。另一个限制