java Google Collections ImmutableMap 迭代顺序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3810738/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Google Collections ImmutableMap iteration order
提问by Peter ?tibrany
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:
我需要结合 Google CollectionImmutableMap
和LinkedHashMap
- 具有定义迭代顺序的不可变地图。似乎 ImmutableMap 本身实际上已经定义了迭代顺序,至少它的文档说:
An immutable, hash-based Map with reliable user-specified iteration order.
一个不可变的、基于哈希的 Map,具有可靠的用户指定的迭代顺序。
However there are no more details. Quick test shows that this might be true, but I want to make sure.
然而,没有更多细节。快速测试表明这可能是真的,但我想确定一下。
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 的迭代顺序吗?如果我这样做ImmutableMap.copyOf(linkedHashMap)
,它的迭代顺序是否与原始链接哈希映射相同?builder 创建的不可变映射怎么样?一些权威答案的链接会有所帮助,因为谷歌没有找到任何有用的东西。(不,来源的链接不算数)。
采纳答案by Jared Levy
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.
更准确地说,ImmutableMap 工厂方法和构建器返回的实例遵循构造映射时提供的输入的迭代顺序。然而,一个 ImmutableSortedMap,它是 ImmutableMap 的子类。对键进行排序。
回答by Peter ?tibrany
I've actually found discussion about this, with answers from library authors:
我实际上已经找到了关于这个的讨论,有图书馆作者的回答:
Kevin Bourrillion: What we mean by "user-specified" is "it can be whatever order you want it to be"; in other words, whatever order you provide the entries to us in the first place, that's the order we use.
Jared Levy: You can also copy a TreeMap or LinkedHashMap that have the desired order.
Kevin Bourrillion:我们所说的“用户指定”是“它可以是你想要的任何顺序”;换句话说,无论您首先向我们提供条目的顺序如何,这就是我们使用的顺序。
Jared Levy:您还可以复制具有所需顺序的 TreeMap 或 LinkedHashMap。
Yes, I should have believed the javadoc, although I think that javadoc can be better in this case. It seems I'm not first who was confused by it. If nothing else, this Q/A will help Google next time someone searches for "ImmutableMap iteration" :-)
是的,我应该相信 javadoc,尽管我认为在这种情况下 javadoc 会更好。似乎我不是第一个被它弄糊涂的人。如果不出意外,这个问答将在下次有人搜索“ImmutableMap 迭代”时帮助 Google :-)
回答by nanda
You should believe the javadoc. If it is not enough, read the source code or report the bug.
你应该相信javadoc。如果还不够,请阅读源代码或报告错误。
A quick view to the source code shows that the map is backed by array and iteration will be done through ImmutableSet that is also backed by an array. So I think the documentation is correct and the order of the elements will be kept as it is.
源代码的快速视图显示映射由数组支持,迭代将通过同样由数组支持的 ImmutableSet 完成。所以我认为文档是正确的,元素的顺序将保持原样。