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

时间:2021-11-25 13:27:58

Is there any difference between a sorted and an ordered collection?

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

7 个解决方案

#1


124  

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

有序集合意味着集合的元素具有特定顺序。订单与价值无关。列表就是一个例子。

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

有序集合意味着集合不仅具有顺序,而且顺序取决于元素的值。 SortedSet就是一个例子。

In contrast, a collection without any order can maintain the elements in any order. A Set is an example.

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

#2


61  

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.

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

#3


11  

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使用“sorted collection”来表示一个集合,例如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").

所以区别在于排序是依赖于值(“已排序”),还是元素独立于其值(“有序”)的属性。

#4


6  

Yes, though the concepts are similar.

是的,虽然概念是相似的。

List is 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是一个有序集合:每个元素都有一个索引,它构成了元素的排序,但通常与元素本身的任何属性无关。

SortedMap and SortedSet are 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.

SortedMap和SortedSet是排序集合,这意味着通过集合的迭代将在从元素本身派生的序列中发生。例如,如果您有SortedSet ,则将根据词典排序顺序对字符串进行排序。

An ordered Collection can be 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()之后)。排序的集合总是隐式排序(即总是有一个“第一”元素,只要你不添加另一个较小的元素,它总是相同的)。

#5


5  

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.

我希望它可以提供帮助。

#6


2  

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.

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

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类上有一些方法可以对列表进行排序。

#7


0  

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

在谈论订单时,通常意味着插入的顺序。排序后可能会更改订单

#1


124  

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

有序集合意味着集合的元素具有特定顺序。订单与价值无关。列表就是一个例子。

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

有序集合意味着集合不仅具有顺序,而且顺序取决于元素的值。 SortedSet就是一个例子。

In contrast, a collection without any order can maintain the elements in any order. A Set is an example.

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

#2


61  

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.

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

#3


11  

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使用“sorted collection”来表示一个集合,例如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").

所以区别在于排序是依赖于值(“已排序”),还是元素独立于其值(“有序”)的属性。

#4


6  

Yes, though the concepts are similar.

是的,虽然概念是相似的。

List is 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是一个有序集合:每个元素都有一个索引,它构成了元素的排序,但通常与元素本身的任何属性无关。

SortedMap and SortedSet are 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.

SortedMap和SortedSet是排序集合,这意味着通过集合的迭代将在从元素本身派生的序列中发生。例如,如果您有SortedSet ,则将根据词典排序顺序对字符串进行排序。

An ordered Collection can be 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()之后)。排序的集合总是隐式排序(即总是有一个“第一”元素,只要你不添加另一个较小的元素,它总是相同的)。

#5


5  

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.

我希望它可以提供帮助。

#6


2  

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.

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

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类上有一些方法可以对列表进行排序。

#7


0  

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

在谈论订单时,通常意味着插入的顺序。排序后可能会更改订单