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

查看:1537
本文介绍了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?

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

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

返回一个不变的映射,其中包含从中提取的键和值 给定的条目(数量不受限制)

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

虽然这种方法类似于针对以下对象的等效varargs API 列出并设置,不幸的是,每个键值对必须为 盒装.一种将键和值装箱的方法,适用于静态 导入,将使此操作更加方便:

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


此外,在 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结果上对UnsupportedOperationException的调用Entry.setValue().

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.

查看全文

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