Java Set保留订单? [英] Java Set retain order?

查看:147
本文介绍了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?

推荐答案

设置 界面不提供任何订购保证。

The Set interface does not provide any ordering guarantees.

其子界面 SortedSet 表示根据某个标准排序的集合。在Java 6中,有两个标准容器实现 SortedSet 。它们是 TreeSet ConcurrentSkipListSet

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天全站免登陆