java – 为什么Collections.sort()仅适用于列表而不适用于集合?

为什么Collections.sort()仅适用于列表而不适用于集合?有什么特别的原因吗?
最佳答案
大多数(但不是全部)Set实现没有顺序概念,因此Collections.sort不支持它们作为一个整体.如果你想要一个具有订单概念的集合,你可以使用像TreeSet这样的东西:

A 07001 implementation based on a 07002. The elements are ordered using their 07003, or by a 07004 provided at set creation time, depending on which constructor is used.

或者LinkedHashSet

Hash table and linked list implementation of the Set interface, with predictable iteration order. This implementation differs from HashSet in that it maintains a doubly-linked list running through all of its entries. This linked list defines the iteration ordering, which is the order in which elements were inserted into the set (insertion-order)

转载注明原文:java – 为什么Collections.sort()仅适用于列表而不适用于集合? - 代码日志