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

标签 java hashmap java-9

来自 Map.java 的文档-

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

但是当我已经可以了use overloaded method ...

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

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

进一步从 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的特征相似 最近推出。

关于java - Map.ofEntries() 而不是 Map.of() 有什么用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64163818/

相关文章:

java - 从 Java 9 访问 com.sun.tools.javac.util

java - 如何在 Spring 中使用自定义 BeanName 创建 bean?

language-agnostic - 什么是哈希表和 HashMap 及其典型用例?

java - Android Studio 无法启动,无法创建 JVM

java - 自定义通用类作为 HashMap 问题的关键

java - 在进行基准测试时,ReadTime 在 HashMap 查找中意味着什么?

java - Eclipse氧气在模块信息文件中给出语法错误警告

java - 如何从 JComboBox 获取文本?

java - 字节扫描仪未正确比较,Java

java - 如何调试这个运行时错误?