Java - 如何创建新条目(键,值)
我想创建类似于 Util.Map.Entry
的新项目,其中将包含结构 key
、value
。
问题是我无法实例化 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
有
公共静态类 AbstractMap.SimpleEntry< K,V>
。不要让名称中的Abstract
部分误导您:它实际上不是一个abstract
类(但它的顶级AbstractMap
是)。事实上,它是一个
static
嵌套类,这意味着您不需要需要一个封闭的AbstractMap
实例来实例化它,所以像这样的东西编译得很好:正如另一个答案中所述,Guava 还有一个方便的
static
工厂方法Maps.immutableEntry
您可以使用。你说:
这并不完全准确。无法直接实例化它的原因(即使用
new
)是因为它是一个Map.Entry 接口
。警告和提示
正如文档中所述,
AbstractMap.SimpleEntry
是@since 1.6
,因此如果您坚持使用 5.0,那么它不可用。要查找另一个
实现 Map.Entry
的已知类,您实际上可以直接转到 javadoc。来自Java 6 版本不幸的是 1.5 版本 没有列出任何您可以使用的已知实现类,因此您可能不得不实现自己的类。
There's
public static class AbstractMap.SimpleEntry<K,V>
. Don't let theAbstract
part of the name mislead you: it is in fact NOT anabstract
class (but its top-levelAbstractMap
is).The fact that it's a
static
nested class means that you DON'T need an enclosingAbstractMap
instance to instantiate it, so something like this compiles fine:As noted in another answer, Guava also has a convenient
static
factory methodMaps.immutableEntry
that you can use.You said:
That's not entirely accurate. The reason why you can't instantiate it directly (i.e. with
new
) is because it's aninterface 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 versionUnfortunately 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.
从 Java 9 开始,有一个新的实用方法允许创建不可变条目,即
Map#entry(Object, Object)
。这是一个简单的示例:
由于它是不可变的,因此调用
setValue
将抛出UnsupportedOperationException
。其他限制是它不可序列化,并且null
作为键或值是被禁止的,如果您不接受,您将需要使用AbstractMap.SimpleImmutableEntry
或AbstractMap.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:
As it is immutable, calling
setValue
will throw anUnsupportedOperationException
. The other limitations are the fact that it is not serializable andnull
as key or value is forbidden, if it is not acceptable for you, you will need to useAbstractMap.SimpleImmutableEntry
orAbstractMap.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 typeMap.of(K key1, V value1, ...)
.您可以自己实现
Map.Entry
接口:然后使用它:
You can just implement the
Map.Entry<K, V>
interface yourself:And then use it:
尝试 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.)AbstractMap.SimpleEntry 示例:
实例化:
添加行:
获取行:
应打印:
非常适合定义图结构的边。就像你大脑中神经元之间的神经元一样。
Example of AbstractMap.SimpleEntry:
Instantiate:
Add rows:
Fetch rows:
Should print:
It's good for defining edges of graph structures. Like the ones between neurons in your head.
你实际上可以选择:
Map.Entry<字符串,字符串> en= Maps.immutableEntry(key, value);
You could actually go with:
Map.Entry<String, String> en= Maps.immutableEntry(key, value);
如果您查看文档 Map.Entry你会发现它是一个静态接口(在Map接口中定义的接口,可以通过Map.Entry访问)并且它有两个实现
类 AbstractMap。 SimpleEntry提供了2个构造函数:
一个示例用例:
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
The class AbstractMap.SimpleEntry provides 2 constructors:
An example use case:
为什么
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
orjava.util.AbstractMap.SimpleEntry
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
implementsjava.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 fluentPair.of(L, R)
syntax.如果您使用 Clojure,您还有另一种选择:
If you are using Clojure, you have another option:
我定义了一个我一直使用的通用 Pair 类。这很棒。作为奖励,通过定义静态工厂方法 (Pair.create),我只需编写类型参数的次数减少一半。
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.