简体   繁体   English

Map.ofEntries() 代替 Map.of() 有什么用

[英]What is the use of Map.ofEntries() instead of Map.of()

From the documentation of Map.java -来自Map.java的文档 -

The Map.of() and Map.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.返回包含从给定条目中提取的键和值的不可变映射,并且条目本身不存储在映射中。

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 中的两个工厂方法之间的主要区别在于:

Map.ofEntries

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 :对于较大数量的条目,将提供一个 API,该 API 将在给定任意数量的键值对的情况下创建一个 Map 实例:

 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.虽然这种方法类似于 List 和 Set 的等效可变参数 API,但不幸的是,它需要对每个键值对进行装箱。 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)

Your assumption about the method .of() from Map is somewhat incorrect probably because while this would compile with Java9:您对 Map 中的 .of .of()方法的假设有些不正确,可能是因为这会用 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 varargs implementation 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.原因是因为List.ofSet.of有一个可变参数实现,但是要为Map创建一个类似的 API,键和值也应该按照 JEP 中的说明进行装箱。 So, the same was created using varargs of type Map.entry() as:因此,使用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.返回包含给定键和值的不可变Map.Entry These entries are suitable for populating Map instances using the Map.ofEntries() method.这些条目适用于使用Map.ofEntries()方法填充 Map 实例。

The Entry instances created by this method have the following characteristics:该方法创建的Entry实例具有以下特点:

  • They disallow null keys and values.它们不允许空键和值。 Attempts to create them using a null key or value result in NullPointerException.尝试使用空键或值创建它们会导致 NullPointerException。

  • They are immutable.它们是不可变的。 Calls to Entry.setValue() on a returned Entry result in UnsupportedOperationException.对返回的 Entry 调用 Entry.setValue() 导致 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.因此,对这些实例的身份敏感操作(引用相等 (==)、身份哈希码和同步)是不可靠的,应该避免。

which are similar to the characteristics of Immutable Map Static Factory Methods introduced recently.类似于最近介绍的Immutable Map静态工厂方法的特性。

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 实例,对于映射,工厂方法的签名是:

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, eg此方法被重载为具有 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"));

Well it's very simple.嗯,这很简单。 Map.of() is not a varargs method. Map.of()不是可变参数方法。 There are only overloaded Map.of() for up to 10 entries.只有最多 10 个条目的重载Map.of() On the other hand, Map.ofEntries() is a varargs method, hence allowing you to specify as many entries as you want.另一方面, Map.ofEntries()是一个可变参数方法,因此允许您根据需要指定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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM