Java Set 保留顺序? [英] Java Set retain order?

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

问题描述

Java Set 是否保留顺序?一个方法正在向我返回一个 Set 并且假设数据是有序的,但是在 Set 上迭代,数据是无序的.有没有更好的方法来管理这个?是否需要更改方法以返回 Set 以外的内容?

Does a Java Set retain order? A method is returning a Set to me and supposedly the data is ordered but iterating over the Set, the data is unordered. Is there a better way to manage this? Does the method need to be changed to return something other than a Set?

推荐答案

Set 接口不提供任何排序​​保证.

The Set interface does not provide any ordering guarantees.

它的子接口 SortedSet 表示根据某种标准排序的集合.在 Java 6 中,有两个标准容器实现了 SortedSet.它们是 TreeSetConcurrentSkipListSet.

Its sub-interface SortedSet represents a set that is sorted according to some criterion. In Java 6, there are two standard containers that implement SortedSet. They are TreeSet and ConcurrentSkipListSet.

除了 SortedSet 接口之外,还有 LinkedHashSet 类.它记住元素插入集合的顺序,并按照该顺序返回其元素.

In addition to the SortedSet interface, there is also the LinkedHashSet class. It remembers the order in which the elements were inserted into the set, and returns its elements in that order.

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

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