java Map.ofEntries() 代替 Map.of() 有什么用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/46601959/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is the use of Map.ofEntries() instead of Map.of()
提问by Mani
From the documentation of Map.java-
来自Map.java的文档-
The
Map.of()
andMap.ofEntries()
static factory methods provide a convenient way to create immutable maps.
在
Map.of()
和Map.ofEntries()
静态工厂方法提供了一种方便的方法来创建不可改变的地图。
But when I already can use overloaded method...
但是当我已经可以使用重载方法时......
Map.of("k1","v1","k2","v2","k3","v3"...);
... what is the use of Map.ofEntries which also
... Map.ofEntries 的用途是什么?
returns an immutable map containing keys and values extracted from the given entries and the entries themselves are not stored in the map.
返回包含从给定条目中提取的键和值的不可变映射,并且条目本身不存储在映射中。
回答by Naman
Any guesses on how would you create a Map of 26 elements?
关于如何创建 26 个元素的 Map 的任何猜测?
The primary difference between the two factory methods in Map that you already linked is that :
您已经链接的 Map 中的两个工厂方法之间的主要区别在于:
Returns an immutable map containing keys and values extracted from the given entries(that are not bounded in count)
返回一个不可变的映射,其中包含从给定条目(不受计数限制)中提取的键和值
From the JEP-269:Convenience Factory Methods for Collections:
来自JEP-269:Convenience Factory Methods for Collections:
For larger numbers of entries, an API will be provided that will create a Map instance given an arbitrary number of key-value pairs:
Map.ofEntries(Map.Entry<K,V>...)
While this approach is analogous to the equivalent varargs APIs for List and Set, it unfortunately requires that each key-value pair be boxed. A method for boxing keys and values, suitable for static import, will make this more convenient:
Map.Entry<K,V> entry(K k, V v)
对于较大数量的条目,将提供一个 API,该 API 将在给定任意数量的键值对的情况下创建一个 Map 实例:
Map.ofEntries(Map.Entry<K,V>...)
虽然这种方法类似于 List 和 Set 的等效可变参数 API,但不幸的是,它需要对每个键值对进行装箱。一种装箱键和值的方法,适用于静态导入,将使这更方便:
Map.Entry<K,V> entry(K k, V v)
Your assumption about the method .of()
from Map is somewhat incorrect probably because while this would compile with Java9:
您对.of()
Map方法的假设有些不正确,可能是因为这会用 Java9 编译:
List<Integer> values = List.of(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10); // note 11 elements here
Set<String> keys = Set.of("z", "o", "tw", "th", "fo", "fi", "si", "se", "e", "n", "te");
This, on the other hand, wouldn't:
另一方面,这不会:
Map<String, Integer> map = Map.of("z", 0, "o", 1,
"tw", 2, "th", 3, "fo", 4, "fi", 5,
"si", 6, "se", 7, "e", 8, "n", 9, "te", 10); // this would not compile
The reason for that is since there is a varargsimplementation for List.of
and Set.of
but to create a similar API for Map
both the keys and values were supposed to be boxed as stated in the JEP as well. So, the same was created using varargs of type Map.entry()
as:
原因是因为有一个varargs实现List.of
,Set.of
但是为Map
键和值创建类似的 API也应该按照 JEP 中的说明进行装箱。因此,使用以下类型的可变参数创建了相同的内容Map.entry()
:
Map<String, Integer> map = Map.ofEntries(Map.entry("z",0),
Map.entry("o",1),Map.entry("t",2)...so on);
Furthermore from the documentation of Map.entry()
which is also introduced Since:9-
此外,从其文档中Map.entry()
还介绍了自:9-
Returns an immutable
Map.Entry
containing the given key and value. These entries are suitable for populating Map instances using theMap.ofEntries()
method.The
Entry
instances created by this method have the following characteristics:
They disallow null keys and values. Attempts to create them using a null key or value result in NullPointerException.
They are immutable. Calls to Entry.setValue() on a returned Entry result in UnsupportedOperationException.
They are not serializable.
They are value-based. Callers should make no assumptions about the identity of the returned instances. This method is free to create new instances or reuse existing ones. Therefore, identity-sensitive operations on these instances (reference equality (==), identity hash code, and synchronization) are unreliable and should be avoided.
返回
Map.Entry
包含给定键和值的不可变对象。这些条目适用于使用该Map.ofEntries()
方法填充 Map 实例 。该
Entry
方法创建的实例具有以下特点:
它们不允许空键和值。尝试使用空键或值创建它们会导致 NullPointerException。
它们是不可变的。对返回的 Entry 调用 Entry.setValue() 导致 UnsupportedOperationException。
它们不可序列化。
它们是基于价值的。调用者不应假设返回实例的身份。此方法可以自由地创建新实例或重用现有实例。因此,对这些实例的身份敏感操作(引用相等 (==)、身份哈希码和同步)是不可靠的,应该避免。
which are similar to the characteristics of Immutable Map Static Factory Methodsintroduced recently.
类似于最近介绍的Immutable Map静态工厂方法的特性。
回答by M Anouti
Well it's very simple. Map.of()
is not a varargs method. There are only overloaded Map.of()
for up to 10 entries. On the other hand, Map.ofEntries()
is a varargs method, hence allowing you to specify as many entries as you want.
这很简单。Map.of()
不是可变参数方法。Map.of()
最多只能重载10 个条目。另一方面,Map.ofEntries()
是一个可变参数方法,因此允许您根据需要指定任意数量的条目。
They could have just added Map.ofEntries()
but since many times you only need just a few entries, they also included the Map.of()
versions as convenience methods so that you don't need to wrap each key-valur pair inside an Entry
.
他们本可以刚刚添加,Map.ofEntries()
但由于很多时候您只需要几个条目,因此他们还将Map.of()
版本作为便捷方法包含在内,因此您无需将每个键值对包装在Entry
.
回答by Sachin Thapa
Java 9 introduced creating small unmodifiable Collection instances using a concise one line code, for maps the signature of factory method is:
Java 9 引入了使用简洁的一行代码创建小的不可修改的 Collection 实例,对于 map,工厂方法的签名是:
static <K,V> Map<K,V> of(K k1, V v1, K k2, V v2, K k3, V v3)
This method is overloaded to have 0 to 10 key-value pairs, e.g.
此方法被重载为具有 0 到 10 个键值对,例如
Map<String, String> map = Map.of("1", "first");
Map<String, String> map = Map.of("1", "first", "2", "second");
Map<String, String> map = Map.of("1", "first", "2", "second", "3", "third");
Similarly you can have up to ten entries.
同样,您最多可以有十个条目。
For a case where we have more than 10 key-value pairs, there is a different method:
对于我们有超过 10 个键值对的情况,有一种不同的方法:
static <K,V> Map<K,V> ofEntries(Map.Entry<? extends K,? extends V>... entries)
Here is the usage.
这是用法。
Map<String, String> map = Map.ofEntries(
new AbstractMap.SimpleEntry<>("1", "first"),
new AbstractMap.SimpleEntry<>("2", "second"),
new AbstractMap.SimpleEntry<>("3", "third"));