Map.ofEntries() 而不是 Map.of() 有什么用

What is the use of Map.ofEntries() instead of Map.of()

来自Map.java-

的文档

The Map.of() and Map.ofEntries() static factory methods provide a convenient way to create immutable maps.

但是当我已经可以时 ...

Map.of("k1","v1","k2","v2","k3","v3"...);

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

猜猜你会如何创建一个包含 26 个元素的地图?

您已经链接的 Map 中的两个工厂方法之间的主要区别在于:

Map.ofEntries

Returns an immutable map containing keys and values extracted from the given entries (that are not bounded in count)

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

您对来自 Map 的方法 .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");

另一方面,这不会:

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

原因是因为有一个 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. So, the same was created using varargs of type Map.entry() 作为:

Map<String, Integer> map = Map.ofEntries(Map.entry("z",0),
       Map.entry("o",1),Map.entry("t",2)...so on);

此外,还介绍了 Map.entry() 的文档 Since:9 -

Returns an immutable Map.Entry containing the given key and value. These entries are suitable for populating Map instances using the Map.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.

与最近介绍的Immutable Map Static Factory Methods的特性相似

嗯,这很简单。 Map.of() 不是可变参数方法。最多只有 10 个条目的重载 Map.of()。另一方面,Map.ofEntries() 是一种可变参数方法,因此允许您指定任意数量的条目。

他们本来可以添加 Map.ofEntries() 但由于很多时候你只需要几个条目,他们还包括 Map.of() 版本作为方便的方法,这样你就不需要包装每个Entry.

中的键值对

Java 9 介绍了使用简洁的一行代码创建小型不可修改的 Collection 实例,对于映射,工厂方法的签名是:

static <K,V> Map<K,V> of(K k1, V v1, K k2, V v2, K k3, V v3)

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

同样,您最多可以有 10 个条目。

对于超过 10 个键值对的情况,有一个不同的方法:

static <K,V> Map<K,V> ofEntries(Map.Entry<? extends K,? extends V>... entries)

这里是用法。

Map<String, String> map = Map.ofEntries(
  new AbstractMap.SimpleEntry<>("1", "first"),
  new AbstractMap.SimpleEntry<>("2", "second"),
  new AbstractMap.SimpleEntry<>("3", "third"));