问题描述
我需要组合Google Collection ImmutableMap
和 LinkedHashMap
—具有已定义迭代顺序的不可变映射。似乎ImmutableMap本身实际上已经定义了迭代顺序,至少:
I need combination of Google Collection ImmutableMap
and LinkedHashMap
— immutable map with defined iteration order. It seems that ImmutableMap itself actually has defined iteration order, at least its documentation says:
但是没有更多细节。快速测试表明这可能是真的,但我想确定。不幸的是,快速查看源代码对我没有多大帮助。
However there are no more details. Quick test shows that this might be true, but I want to make sure. Unfortunately quick look into the sources didn't help me very much.
我的问题是:我可以依赖ImmutableMap的迭代顺序吗?如果我执行 ImmutableMap.copyOf(linkedHashMap)
,它是否与原始链接哈希映射具有相同的迭代顺序?构建器创建的不可变映射怎么样?一些权威答案的链接会有所帮助,因为谷歌没有找到任何有用的东西。 (不,链接到源不计)。
My question is: can I rely on iteration order of ImmutableMap? If I do ImmutableMap.copyOf(linkedHashMap)
, will it have same iteration order as original linked hash map? What about immutable maps created by builder? Some link to authoritative answer would help, since Google didn't find anything useful. (And no, links to the sources don't count).
推荐答案
更准确地说,ImmutableMap工厂方法和构建器返回遵循构造中的映射时提供的输入的迭代顺序的实例。但是,一个ImmutableSortedMap,它是ImmutableMap的子类。对键进行排序。
To be more precise, the ImmutableMap factory methods and builder return instances that follow the iteration order of the inputs provided when the map in constructed. However, an ImmutableSortedMap, which is a subclass of ImmutableMap. sorts the keys.
这篇关于Google Collections ImmutableMap迭代顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!