- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
来自 Map.java 的文档-
The
Map.of()
andMap.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.
最佳答案
您已经链接的 Map 中的两个工厂方法之间的主要区别在于:
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 theMap.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/46601959/
来自 Map.java 的文档- The Map.of() and Map.ofEntries() static factory methods provide a convenient way to
来自 Map.java 的文档- The Map.of() and Map.ofEntries() static factory methods provide a convenient way to
这个问题在这里已经有了答案: How to get a immutable collection from java HashMap? (5 个答案) 关闭 2 年前。 我想使用 Java 9 中的
我是一名优秀的程序员,十分优秀!