Map.ofEntries() 代替 Map.of() 有什么用 [英] What is the use of Map.ofEntries() instead of Map.of()

查看:46
本文介绍了Map.ofEntries() 代替 Map.of() 有什么用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

来自 Map.java -

Map.of()Map.ofEntries() 静态工厂方法提供了一种创建不可变地图的便捷方式.

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

但是当我已经可以 使用重载方法 ...

But when I already can use overloaded method ...

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

... Map.ofEntries 有什么用呢

... what is the use of Map.ofEntries which also

返回一个不可变映射,其中包含从给定条目中提取的键和值,并且条目本身不存储在映射中.

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

The primary difference between the two factory methods in Map that you already linked is that :

Map.ofEntries

返回一个不可变的映射,其中包含从给定条目(数量不限)

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

来自 JEP-269:集合的便利工厂方法:

对于大量条目,将提供一个 API,该 API 将给定任意数量的键值对,创建一个 Map 实例:

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

虽然这种方法类似于等效的可变参数 APIList 和 Set,不幸的是它要求每个键值对是盒装.一种对键和值进行装箱的方法,适用于静态导入,会更方便:

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 编译:


Your assumption about the method .of() from Map is somewhat incorrect probably because while this would compile with 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 实现 List.ofSet.of 但要为 Map 创建类似的 API 键和值都应该按照 JEP 中的说明装箱.因此,同样是使用 Map.entry() 为:

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. So, the same was created using varargs of type Map.entry() as:

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

<小时>

进一步来自 Map.entry() 的文档 Since:9 -

返回包含给定键和值的不可变 Map.Entry.这些条目适用于使用Map.ofEntries() 方法.

Returns an immutable Map.Entry containing the given key and value. These entries are suitable for populating Map instances using the Map.ofEntries() method.

此方法创建的 Entry 实例具有以下特点:

The Entry instances created by this method have the following characteristics:

  • 它们不允许空键和值.尝试使用 null 键或值创建它们会导致 NullPointerException.

  • They disallow null keys and values. Attempts to create them using a null key or value result in NullPointerException.

它们是不可变的.对返回的 Entry 调用 Entry.setValue() 会导致 UnsupportedOperationException.

They are immutable. Calls to Entry.setValue() on a returned Entry result in UnsupportedOperationException.

它们不可序列化.

它们是基于价值的.调用者不应对返回的实例的身份做出任何假设.此方法可免费新建实例或重用现有实例.因此,身份敏感对这些实例的操作(引用相等 (==)、身份哈希代码和同步)是不可靠的,应该避免.

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.

这篇关于Map.ofEntries() 代替 Map.of() 有什么用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆