Google Collections ImmutableMap 迭代顺序
我需要 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:
一个不可变的、基于散列的 Map,具有可靠的用户指定的迭代顺序.
An immutable, hash-based Map with reliable user-specified iteration order.
但是没有更多细节.快速测试表明这可能是真的,但我想确定一下.
However there are no more details. Quick test shows that this might be true, but I want to make sure.
我的问题是:我可以依赖 ImmutableMap 的迭代顺序吗?如果我执行 ImmutableMap.copyOf(linkedHashMap)
,它的迭代顺序是否与原始链接哈希映射相同?由builder创建的不可变地图呢?一些权威答案的链接会有所帮助,因为谷歌没有发现任何有用的东西.(不,源链接不算在内).
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.
相关文章