简体   繁体   English

ImmutableMap.of 与 Java Map.of

[英]ImmutableMap.of vs Java Map.of

I have seen that ImmutableMap.of is used to create immutable map object.我已经看到 ImmutableMap.of 用于创建不可变的地图对象。 At the same time Java offers Map.of which creates immutable map as well.同时,Java 提供了 Map.of,它也创建了不可变的地图。 Is there any advantage of using ImmutableMap.of instead of Map.of from Java ?使用 ImmutableMap.of 代替 Java 中的 Map.of 有什么好处吗?

Edit: I am not asking differences between map and immutable map.编辑:我不是在问地图和不可变地图之间的区别。 I am asking why some people prefer ImmutableMap.of instead of Map.of which also creates immutableMap.我在问为什么有些人更喜欢 ImmutableMap.of 而不是 Map.of,它也创建了 immutableMap。

A possible downside of Map.of is that it returns Map , which provides a lot of methods that are documented to throw UnsupportedOperationException in case the map is immutable - that may be considered a disadvantage in API design. Map.of的一个可能的缺点是它返回Map ,它提供了许多记录在案的方法,以在地图不可变的情况下抛出UnsupportedOperationException ——这可能被认为是 API 设计中的一个缺点。 There is no reliable way for code consuming Map s to know whether any of these will work.没有可靠的方法让使用Map的代码知道其中任何一个是否有效。

There were good reasons for this decision, such as being able to take advantage of the existing codebase without breaking backwards compatibility.这个决定有充分的理由,例如能够在不破坏向后兼容性的情况下利用现有的代码库。 But this comes at the cost that the type system can't help with communicating whether a given method can accept (or requires) an immutable or a mutable map.但这是以类型系统无法帮助传达给定方法是否可以接受(或需要)不可变或可变映射的代价。 That is left to documentation.这留给文档。

( guavas ImmutableMap also implements Map , but at least the unsupported methods are marked as deprecated. Arguably still not ideal, but... tradeoffs) 番石榴ImmutableMap也实现了Map ,但至少不支持的方法被标记为已弃用。可以说仍然不理想,但是......权衡)

I assume you are talking about com.google.common.collect.ImmutableMap ?我假设您在谈论com.google.common.collect.ImmutableMap According to the javadoc , that has been in guava since 2.0, which is really old (I can only find the year 2010 for version 3.0 , so 2.0 is before that). 根据 javadoc ,自 2.0 以来一直在 guava 中,这真的很旧(我只能找到2010 年的版本 3.0 ,所以 2.0 在那之前)。

Map.of was only introduced with java 9, which was released in September 2017 . Map.of在 2017 年 9 月发布的java 9 中引入。 That means that only the first option was available before java 9, but you needed a library.这意味着在 java 9 之前只有第一个选项可用,但您需要一个库。

Since this feature was finally integrated into java itself with java 9 and you don't need an external library anymore, I would argue Map.of is preferrable.由于此功能最终通过 java 9 集成到 java 本身中,并且您不再需要外部库,因此我认为Map.of是可取的。

However, in terms of readability, I have always hated that Map.of as well as List.of return immutable implementations, which is not what an inexperienced (=has not fallen into that trap yet) developer expects.但是,就可读性而言,我一直讨厌Map.ofList.of返回不可变的实现,这不是没有经验(=尚未落入那个陷阱)开发人员所期望的。

If you are using guava anyways, ImmutableMap.of is the more readable option, but that's up to you.如果您仍然使用番石榴,则ImmutableMap.of是更具可读性的选项,但这取决于您。

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

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