简体   繁体   English

Java:Map.of<"k1", "v1"> vs Collections.singletonMap("k1", "v1")

[英]Java: Map.of<"k1", "v1"> vs Collections.singletonMap("k1", "v1")

Are there any differences between Map.of("key", "value") and Collections.singletonMap("key", "value") ? Map.of("key", "value")Collections.singletonMap("key", "value")之间有什么区别吗?

Are there any practices on which one is to be preferred over another if I just need to store one mapping for Java 9 and above?如果我只需要为 Java 9 及更高版本存储一个映射,是否有任何实践优于另一个?

Map.of地图.of

Map.of can only be used in Java 9 and above. Map.of只能在 Java 9 及更高版本中使用。 It can be used to store 0 to 10 key-value pairs.它可用于存储 0 到 10 个键值对。 See https://docs.oracle.com/javase/9/docs/api/java/util/Map.html#of-- and following.请参阅https://docs.oracle.com/javase/9​​/docs/api/java/util/Map.html#of--和以下内容。 See also https://docs.oracle.com/javase/9/docs/api/java/util/Map.html#immutable :另请参阅https://docs.oracle.com/javase/9​​/docs/api/java/util/Map.html#immutable

The Map.of() and Map.ofEntries() static factory methods provide a convenient way to create immutable maps. Map.of() 和 Map.ofEntries() 静态工厂方法提供了一种创建不可变映射的便捷方式。 The Map instances created by these methods have the following characteristics:这些方法创建的 Map 实例具有以下特点:

  • They are structurally immutable.它们在结构上是不可变的。 Keys and values cannot be added, removed, or updated.无法添加、删除或更新键和值。 Calling any mutator method will always cause UnsupportedOperationException to be thrown.调用任何 mutator 方法总是会导致抛出 UnsupportedOperationException。 However, if the contained keys or values are themselves mutable, this may cause the Map to behave inconsistently or its contents to appear to change.但是,如果包含的键或值本身是可变的,这可能会导致 Map 的行为不一致或其内容似乎发生了变化。
  • They disallow null keys and values.它们不允许空键和值。 Attempts to create them with null keys or values result in NullPointerException.尝试使用 null 键或值创建它们会导致 NullPointerException。
  • They are serializable if all keys and values are serializable.如果所有键和值都是可序列化的,则它们是可序列化的。
  • They reject duplicate keys at creation time.他们在创建时拒绝重复的密钥。 Duplicate keys passed to a static factory method result in IllegalArgumentException.传递给静态工厂方法的重复键会导致 IllegalArgumentException。
  • The iteration order of mappings is unspecified and is subject to change.映射的迭代顺序未指定,可能会发生变化。 They are value-based.它们是基于价值的。 Callers should make no assumptions about the identity of the returned instances.调用者不应对返回的实例的身份做出任何假设。 Factories are 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.因此,对这些实例的身份敏感操作(引用相等 (==)、身份哈希码和同步)是不可靠的,应该避免。 They are serialized as specified on the Serialized Form page.它们按照序列化表单页面上的指定进行序列化。

Similarily, there is List.of and Set.of .同样,还有List.ofSet.of

Collections.singletonMap Collections.singletonMap

Collections.singletonMap is available since Java 1.3. Collections.singletonMap从 Java 1.3 开始可用。 It can only be used to store one key-value pair.它只能用于存储一个键值对。 See https://docs.oracle.com/javase/9/docs/api/java/util/Collections.html#singletonMap-KV- .请参阅https://docs.oracle.com/javase/9​​/docs/api/java/util/Collections.html#singletonMap-KV-

In general, singletons always contain exactly one value.一般来说,单例总是只包含一个值。 A singleton class is one that has exactly one instance.单例类是只有一个实例的类。 A singleton map has exactly one mapping, a singleton list has exactly one element and so on.单例映射只有一个映射,单例列表只有一个元素,依此类推。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM