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

发布于 2024-09-06 17:28:23 字数 182 浏览 4 评论 0原文

我想创建类似于 Util.Map.Entry 的新项目,其中将包含结构 keyvalue

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

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

I'd like to create new item that similarly to Util.Map.Entry that will contain the structure key, value.

The problem is that I can't instantiate a Map.Entry because it's an interface.

Does anyone know how to create a new generic key/value object for Map.Entry?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(11

魔法唧唧 2024-09-13 17:28:23

公共静态类 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)是因为它是一个 Map.Entry 接口


警告和提示

正如文档中所述,AbstractMap.SimpleEntry@since 1.6,因此如果您坚持使用 5.0,那么它不可用。

要查找另一个实现 Map.Entry 的已知类,您实际上可以直接转到 javadoc。来自Java 6 版本

接口映射.Entry

所有已知的实现类


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

There's public static class AbstractMap.SimpleEntry<K,V>. Don't let the Abstract part of the name mislead you: it is in fact NOT an abstract class (but its top-level AbstractMap is).

The fact that it's a static nested class means that you DON'T need an enclosing AbstractMap instance to instantiate it, so something like this compiles fine:

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

As noted in another answer, Guava also has a convenient static factory method Maps.immutableEntry that you can use.


You said:

I can't use Map.Entry itself because apparently it's a read-only object that I can't instantiate new instanceof

That's not entirely accurate. The reason why you can't instantiate it directly (i.e. with new) is because it's an interface Map.Entry.


Caveat and tip

As noted in the documentation, AbstractMap.SimpleEntry is @since 1.6, so if you're stuck to 5.0, then it's not available to you.

To look for another known class that implements Map.Entry, you can in fact go directly to the javadoc. From the Java 6 version

Interface Map.Entry

All Known Implementing Classes:

Unfortunately the 1.5 version does not list any known implementing class that you can use, so you may have be stuck with implementing your own.

柳絮泡泡 2024-09-13 17:28:23

从 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 ,...)

Starting from Java 9, there is a new utility method allowing to create an immutable entry which is Map#entry(Object, Object).

Here is a simple example:

Entry<String, String> entry = Map.entry("foo", "bar");

As it is immutable, calling setValue will throw an UnsupportedOperationException. The other limitations are the fact that it is not serializable and null as key or value is forbidden, if it is not acceptable for you, you will need to use AbstractMap.SimpleImmutableEntry or AbstractMap.SimpleEntry instead.

NB: If your need is to create directly a Map with 0 to up to 10 (key, value) pairs, you can instead use the methods of type Map.of(K key1, V value1, ...).

堇年纸鸢 2024-09-13 17:28:23

您可以自己实现 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;
    }
}

然后使用它:

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

You can just implement the Map.Entry<K, V> interface yourself:

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

And then use it:

Map.Entry<String, Object> entry = new MyEntry<String, Object>("Hello", 123);
System.out.println(entry.getKey());
System.out.println(entry.getValue());
无人接听 2024-09-13 17:28:23

尝试 Maps.immutableEntry 来自 Guava

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

Try Maps.immutableEntry from Guava

This has the advantage of being compatible with Java 5 (unlike AbstractMap.SimpleEntry which requires Java 6.)

来日方长 2024-09-13 17:28:23

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

非常适合定义图结构的边。就像你大脑中神经元之间的神经元一样。

Example of AbstractMap.SimpleEntry:

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

Instantiate:

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

Add rows:

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

Fetch rows:

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

Should print:

2
3
20
30
2
4

It's good for defining edges of graph structures. Like the ones between neurons in your head.

漆黑的白昼 2024-09-13 17:28:23

你实际上可以选择:
Map.Entry<字符串,字符串> en= Maps.immutableEntry(key, value);

You could actually go with:
Map.Entry<String, String> en= Maps.immutableEntry(key, value);

臻嫒无言 2024-09-13 17:28:23

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

所有已知的实现类:
AbstractMap.SimpleEntry、AbstractMap.SimpleImmutableEntry

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

构造函数和描述
AbstractMap.SimpleEntry(K键, V值)
创建一个条目,表示从指定键到
的映射
指定值。
AbstractMap.SimpleEntry(Map.Entryentry)
创建一个表示与指定条目相同映射的条目。

一个示例用例:

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

If you look at the documentation of Map.Entry you will find that it is a static interface (an interface which is defined inside the Map interface an can be accessed through Map.Entry) and it has two implementations

All Known Implementing Classes:
AbstractMap.SimpleEntry, AbstractMap.SimpleImmutableEntry

The class AbstractMap.SimpleEntry provides 2 constructors:

Constructors and Description
AbstractMap.SimpleEntry(K key, V value)
Creates an entry representing a mapping from the specified key to the
specified value.
AbstractMap.SimpleEntry(Map.Entry<? extends K,? extends V> entry)
Creates an entry representing the same mapping as the specified entry.

An example use case:

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());
    }
}
过去的过去 2024-09-13 17:28:23

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

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

Why Map.Entry? I guess something like a key-value pair is fit for the case.

Use java.util.AbstractMap.SimpleImmutableEntry or java.util.AbstractMap.SimpleEntry

妥活 2024-09-13 17:28:23

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

正如其他人提到的,Guava 的 com.google.common.collect.Maps.immutableEntry(K, V) 可以解决这个问题。

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

org.apache.commons.lang3.tuple.Pair implements java.util.Map.Entry and can also be used standalone.

Also as others mentioned Guava's com.google.common.collect.Maps.immutableEntry(K, V) does the trick.

I prefer Pair for its fluent Pair.of(L, R) syntax.

假情假意假温柔 2024-09-13 17:28:23

如果您使用 Clojure,您还有另一种选择:

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

If you are using Clojure, you have another option:

(defn map-entry
  [k v]
  (clojure.lang.MapEntry/create k v))
贵在坚持 2024-09-13 17:28:23

我定义了一个我一直使用的通用 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);
    }

}

I defined a generic Pair class that I use all the time. It's great. As a bonus, by defining a static factory method (Pair.create) I only have to write the type arguments half as often.

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

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文