我相信有一个很好的理由,但有人能解释一下为什么java.util.Set接口缺少get(int Index),或任何类似的get()方法吗?

集合似乎很适合把东西放进去,但我找不到一种优雅的方法来从中检索单个项目。

如果我知道我想要的第一项,我可以使用set.iterator().next(),但否则,似乎我必须强制转换到一个数组来检索特定索引的项?

从集合中检索数据的适当方法是什么?(与使用迭代器不同)

我相信它被排除在API之外的事实意味着有一个很好的理由不这样做——有人能启发我吗?

编辑: 这里有一些非常棒的回答,还有一些说“更多的背景”。具体的场景是一个dbUnit测试,在这个测试中,我可以合理地断言从查询返回的集合只有1个项,并且我正在尝试访问该项。

然而,这个问题在没有场景的情况下更有效,因为它仍然更集中:

set和list的区别是什么?

感谢大家的精彩回答。


当前回答

我不确定是否有人这么说过,但你需要明白以下几点:

集合中没有“first”元素。

因为,正如其他人所说,集合没有顺序。集合是一个数学概念,特别不包括排序。

Of course, your computer can't really keep a list of stuff that's not ordered in memory. It has to have some ordering. Internally it's an array or a linked list or something. But you don't really know what it is, and it doesn't really have a first element; the element that comes out "first" comes out that way by chance, and might not be first next time. Even if you took steps to "guarantee" a particular first element, it's still coming out by chance, because you just happened to get it right for one particular implementation of a Set; a different implementation might not work that way with what you did. And, in fact, you may not know the implementation you're using as well as you think you do.

People run into this ALL. THE. TIME. with RDBMS systems and don't understand. An RDBMS query returns a set of records. This is the same type of set from mathematics: an unordered collection of items, only in this case the items are records. An RDBMS query result has no guaranteed order at all unless you use the ORDER BY clause, but all the time people assume it does and then trip themselves up some day when the shape of their data or code changes slightly and triggers the query optimizer to work a different way and suddenly the results don't come out in the order they expect. These are typically the people who didn't pay attention in database class (or when reading the documentation or tutorials) when it was explained to them, up front, that query results do not have a guaranteed ordering.

其他回答

如果您不介意要排序的集合,那么您可能有兴趣看看索引树映射项目。

增强的TreeSet/TreeMap提供了通过索引访问元素或获取元素索引的功能。实现基于更新RB树中的节点权重。所以这里没有迭代,也没有列表备份。

只是补充一点迈尔斯的回答中没有提到的。

如果我知道我想要第一项,我就可以 使用set.iterator().next(),但是 不然的话,我只好放弃了 数组中检索项 具体指标? 合适的方式是什么 从集合中检索数据?(其他 而不是使用迭代器)

您还应该熟悉SortedSet接口(其最常见的实现是TreeSet)。

SortedSet是一个集合(即元素是唯一的),它通过元素的自然顺序或使用一些比较器保持有序。可以使用first()和last()方法轻松访问第一个和最后一个项。SortedSet每隔一段时间就会派上用场,当您需要保持集合无重复且以某种方式有序时。

编辑:如果你需要一个Set,它的元素按照插入顺序保存(很像List),看一下LinkedHashSet。

我不确定是否有人这么说过,但你需要明白以下几点:

集合中没有“first”元素。

因为,正如其他人所说,集合没有顺序。集合是一个数学概念,特别不包括排序。

Of course, your computer can't really keep a list of stuff that's not ordered in memory. It has to have some ordering. Internally it's an array or a linked list or something. But you don't really know what it is, and it doesn't really have a first element; the element that comes out "first" comes out that way by chance, and might not be first next time. Even if you took steps to "guarantee" a particular first element, it's still coming out by chance, because you just happened to get it right for one particular implementation of a Set; a different implementation might not work that way with what you did. And, in fact, you may not know the implementation you're using as well as you think you do.

People run into this ALL. THE. TIME. with RDBMS systems and don't understand. An RDBMS query returns a set of records. This is the same type of set from mathematics: an unordered collection of items, only in this case the items are records. An RDBMS query result has no guaranteed order at all unless you use the ORDER BY clause, but all the time people assume it does and then trip themselves up some day when the shape of their data or code changes slightly and triggers the query optimizer to work a different way and suddenly the results don't come out in the order they expect. These are typically the people who didn't pay attention in database class (or when reading the documentation or tutorials) when it was explained to them, up front, that query results do not have a guaranteed ordering.

我遇到了这样的情况,我实际上想要一个通过索引访问的SortedSet(我同意其他海报,访问一个没有索引的无序集是没有意义的)。 例如,在树中,我希望对子节点进行排序,不允许重复子节点。

我需要通过索引访问来显示它们,set属性可以有效地消除重复。

在java中找不到合适的集合。util或谷歌集合,我发现它直接实现它自己。基本思想是包装SortedSet并在需要通过索引访问时创建一个List(并且在SortedSet被更改时忘记该列表)。当然,这只有在更改包装的SortedSet和在集合的生命周期中分离访问列表时才有效。否则它就像一个经常排序的列表,也就是说太慢了。

对于大量的子节点,这大大提高了我通过Collections.sort进行排序的列表的性能。

这就引出了一个问题,什么时候应该使用集合,什么时候应该使用列表。通常,建议是这样的:

如果需要有序数据,请使用List 如果你需要唯一的数据,使用一个集合 如果两者都需要,可以使用SortedSet(用于按比较器排序的数据)或OrderedSet/UniqueList(用于按插入排序的数据)。不幸的是,Java API还没有OrderedSet/UniqueList。

第四种经常出现的情况是,两者都不需要。在这种情况下,你会看到一些程序员使用列表,一些使用集合。就我个人而言,我觉得把set看作一个没有顺序的列表是非常有害的——因为它真的是另一种野兽。除非你需要集唯一性或集相等性,否则总是倾向于列表。