Ruby 中的 Set 是否始终保留插入顺序? [英] Does Set in Ruby always preserve insertion order?

查看:48
本文介绍了Ruby 中的 Set 是否始终保留插入顺序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

即,Ruby 的 Set 是否等同于 Java 的 LinkedHashSet?

i.e., is Ruby's Set equivalent to Java's LinkedHashSet?

推荐答案

在 Ruby 1.9 中:.在 Ruby 1.8 中:可能不是.

In Ruby 1.9: yes. In Ruby 1.8: probably not.

Set 使用Hash 内部;并且由于哈希在 1.9 中是按插入顺序排列的,因此您可以开始使用了!

Set uses a Hash internally; and since hashes are insertion-ordered in 1.9, you're good to go!

正如 mu 太短所指出的,这是一个实现细节,可能会在未来(虽然不太可能).值得庆幸的是,Set 的当前实现是纯 ruby​​ 的,如果您愿意,将来可以改编成 OrderedSet

As mu is too short points out, this is an implementation detail and could change in the future (though unlikely). Thankfully, the current implementation of Set is pure ruby, and could be adapted into an OrderedSet in the future if you like

这篇关于Ruby 中的 Set 是否始终保留插入顺序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆