简体   繁体   English

Scala 映射中的键顺序

[英]the order of keys in scala map

i need to get the keys from map in the order when they are added.我需要按添加顺序从地图中获取密钥。 but seems the map.keys return a array with re-ordered keys.但似乎 map.keys 返回一个带有重新排序键的数组。

how to keep the order of the key/values as they added when retrieve them ?如何在检索键/值时保持它们添加的顺序?

Thanks谢谢

Maps implemented using trees don't store their keys in the order that they are inserted.使用树实现的映射不会按照插入的顺序存储它们的键。 That information is lost when it's inserted by value (based on its Comparable ), and further "randomized" when the tree is balanced.该信息在按值插入时会丢失(基于其Comparable ),并在树平衡时进一步“随机化”。

As @mziccard pointed out, linear structures (such as a LinkedHashMap ) can preserve insertion order, so that's the way to go if you need such a capability.正如@mziccard 指出的那样,线性结构(例如LinkedHashMap )可以保留插入顺序,因此如果您需要这样的功能,这就是要走的路。

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

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