Java 有序集合和排序集合有什么区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1084146/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 23:24:01  来源:igfitidea点击:

What is the difference between an ordered and a sorted collection?

javacollections

提问by Johanna

Is there any difference between a sortedand an orderedcollection?

排序集合和有序集合之间有什么区别吗?

回答by nos

An ordered collection maintains the order of the elements based on the sequence you put stuff into/remove them from the collection.

有序集合根据您将内容放入/从集合中删除的顺序来维护元素的顺序。

A sorted collection keeps the elements sorted based on a sort criteria.

排序集合根据排序条件对元素进行排序。

回答by g .

An ordered collectionmeans that the elements of the collection have a specific order. The order is independent of the value. A Listis an example.

一个有序集合意味着该集合的元素有一个特定的顺序。顺序与值无关。甲列表是一个例子。

A sorted collectionmeans that not only does the collection have order, but the order depends on the value of the element. A SortedSetis an example.

一个分类收集的手段,不仅这个集合中有顺序,但顺序取决于元素的值。甲SortedSet的就是一个例子。

In contrast, a collection without any ordercan maintain the elements in any order. A Setis an example.

相反,没有任何顺序集合可以以任何顺序维护元素。一就是一个例子。

回答by Steve Jessop

Java uses "ordered collection" to mean a collection such as List, where (unlike HashSet), the collection remembers what order the elements are supposed to be in. So elements can be added to the collection at a particular "place" in the order.

Java 使用“有序集合”来表示诸如 List 之类的集合,其中(与 HashSet 不同),集合会记住元素应该处于的顺序。因此可以将元素添加到集合中的特定“位置”中的顺序.

Java uses "sorted collection" to mean a collection such as SortedSet, where (unlike List), the order that the iterator traverses the collection is in accordance with a specified Comparator or the natural order of the elements.

Java用“有序集合”来表示像SortedSet这样的集合,其中(与List不同),迭代器遍历集合的顺序是按照指定的Comparator或者元素的自然顺序。

So the difference is whether the ordering depends on the values ("sorted"), or is a property that elements have independently of their value ("ordered").

因此,区别在于排序是取决于值(“已排序”),还是元素具有独立于其值(“已排序”)的属性。

回答by Yishai

Sorted would imply ordering according to an implementation of Comparable or Comparator. Ordered would imply that it is following the insertion order or some other definition of order that is consistent and defined, but otherwise arbitrary.

Sorted 意味着根据 Comparable 或 Comparator 的实现进行排序。有序意味着它遵循插入顺序或其他一些一致和定义的顺序定义,但在其他方面是任意的。

So a sorted list of strings would be sorted according to the String.compareTo method. A list might contain a list of strings inserted in arbitrary order, but that order will always remain the same.

因此,将根据 String.compareTo 方法对已排序的字符串列表进行排序。列表可能包含以任意顺序插入的字符串列表,但该顺序将始终保持不变。

Of course there are methods on the Collections class to sort a list.

当然,Collections 类中有一些方法可以对列表进行排序。

回答by Michael Borgwardt

Yes, though the concepts are similar.

是的,虽然概念相似。

Listis an ordered collection: each element has an index, which forms an ordering of the elements, but not usually related to any property of the elements themselves.

List是一个有序集合:每个元素都有一个索引,它形成元素的排序,但通常与元素本身的任何属性无关。

SortedMapand SortedSetare sorted collections, which means that iteration through the collection will happen in a sequence derived from the elements themselves. For example, if you have a SortedSet<String>then the Strings will be sorted according to the lexicographical sort order.

SortedMapSortedSet是排序集合,这意味着通过集合的迭代将按照从元素本身派生的序列发生。例如,如果您有一个,SortedSet<String>那么字符串将根据字典排序顺序进行排序。

An ordered Collection canbe sorted but doesn't have to be (e.g. after using Collections.sort()) when the external ordering is identical with the elements' sort order. A sorted collection is always implicitly ordered (i.e. there is always a "first" element, and it's always the same as long as you don't add another, smaller one).

当外部排序与元素的排序顺序相同时,有序集合可以排序但不必排序(例如在使用之后Collections.sort())。排序集合总是隐式排序的(即总是有一个“第一个”元素,只要你不添加另一个更小的元素,它总是相同的)。

回答by gvalenncia

An ordered collection is a collection that keep track of a consecutive index which every element is inserted in.

有序集合是跟踪每个元素插入的连续索引的集合。

A sorted collection is an ordered collection when the order additionally depends on the value of the element to be inserted in, throughout the use of the Comparable interface which provides you with a method to define the sorting criteria.

排序集合是有序集合,当顺序另外取决于要插入的元素的值时,在 Comparable 接口的整个使用过程中,该接口为您提供了定义排序标准的方法。

I hope it could help.

我希望它能有所帮助。

回答by sotondolphin

A sorted collection usually mean the elements are sorted from minimun value to maxinum value or vice versa depending on the attribute(s) of the elements on which algorithms work.

排序集合通常意味着元素从最小值到最大值排序,反之亦然,这取决于算法工作的元素的属性。

for a interger collections, the sorted may be from min number to max number for a person collection, it may be sored by the height of persons or the weight of persons, etc.

对于整数集合,对于一个人集合,排序可能是从最小数到最大数,也可能是按人的身高或人的体重等排序。

When talking about order, it usually means the order of insertion. The order may be changed after sorting

说到顺序,通常是指插入的顺序。排序后可能会更改顺序

回答by hardikhirapara

Sorted Collectionvs. Ordered Collection

分类收集有序集合

1. Sorted collection

1. 排序集合

A sorted collection is sorting a collection by utilizing the sorting features provided by the Java collections framework. The sorting occurs in the memory of JVM which running Hibernate, after the data being read from database using java comparator.

排序集合是利用 Java 集合框架提供的排序功能对集合进行排序。在使用 java 比较器从数据库读取数据后,排序发生在运行 Hibernate 的 JVM 的内存中。

If your collection is not large, it will be more efficient way to sort it. As it happens in jvm memory, it can throw Out of Memory error.

如果您的收藏量不大,排序将是更有效的方式。当它发生在 jvm 内存中时,它可能会抛出 Out of Memory 错误。

2. Order collection

2. 订单收集

Order collection is sorting a collection by specifying the order-by clause in query for sorting this collection when retrieval. If your collection is very large, it will be more efficient way to sort it. It is fast compared to sorted collection.

订单集合是通过在查询中指定 order-by 子句来对集合进行排序,以便在检索时对该集合进行排序。如果您的集合非常大,排序将是更有效的方式。与排序集合相比,它更快。