在Java中有SortedSet和SortedMap接口。两者都属于Java Collections框架,并提供了一种访问元素的排序方式。

然而,在我的理解中,Java中没有SortedList。您可以使用java.util.Collections.sort()对列表进行排序。

知道它为什么是这样设计的吗?


当前回答

另一点是插入操作的时间复杂度。 对于列表插入,期望复杂度为O(1)。 但是,排序的列表并不能保证这一点。

最重要的一点是,列表对它们的元素没有任何假设。 例如,您可以列出不实现等于或比较的东西。

其他回答

Set和Map是非线性数据结构。列表是线性数据结构。


树数据结构SortedSet和SortedMap接口使用常用的红黑树实现算法分别实现TreeSet和TreeMap。因此,它确保没有重复的项(或Map情况下的键)。

List已经维护了有序的集合和基于索引的数据结构,树不是基于索引的数据结构。 树根据定义不能包含重复项。 在List中,我们可以有副本,所以没有TreeList(即。没有SortedList)。 List按插入顺序维护元素。因此,如果我们想对列表进行排序,就必须使用java.util.Collections.sort()。它根据元素的自然顺序,将指定的列表按升序排序。

因为所有列表都已经按照条目的添加顺序(FIFO顺序)“排序”了,所以可以使用java.util.Collections.sort()使用另一种顺序“使用”它们,包括元素的自然顺序。

编辑:

列表作为数据结构的基础是有趣的是插入项的顺序。

集合没有这个信息。

如果您想通过添加时间来排序,请使用List。如果您想按其他标准排序,请使用SortedSet。

我认为以上都不能回答这个问题,原因如下:

Since same functionality can be achieved by using other collections such as TreeSet, Collections, PriorityQueue..etc (but this is an alternative which will also impose their constraints i.e. Set will remove duplicate elements. Simply saying even if it does not impose any constraint, it does not answer the question why SortedList was not created by java community) Since List elements do not implements compare/equals methods (This holds true for Set & Map also where in general items do not implement Comparable interface but when we need these items to be in sorted order & want to use TreeSet/TreeMap,items should implement Comparable interface) Since List uses indexing & due to sorting it won't work (This can be easily handled introducing intermediate interface/abstract class)

但没有人告诉它背后的确切原因,因为我相信这类问题最好由java社区自己来回答,因为它只有一个具体的答案,但让我尽我所能回答如下:

我们知道排序是一个昂贵的操作,列表和Set/Map之间有一个基本的区别,列表可以有重复,而Set/Map不能。 这就是我们为Set/Map提供TreeSet/TreeMap形式的默认实现的核心原因。在内部,这是一个红黑树,每个操作(插入/删除/搜索)的复杂度为O(log N),其中由于重复列表无法适合这种数据存储结构。

Now the question arises we could also choose a default sorting method for List also like MergeSort which is used by Collections.sort(list) method with the complexity of O(N log N). Community did not do this deliberately since we do have multiple choices for sorting algorithms for non distinct elements like QuickSort, ShellSort, RadixSort...etc. In future there can be more. Also sometimes same sorting algorithm performs differently depending on the data to be sorted. Therefore they wanted to keep this option open and left this on us to choose. This was not the case with Set/Map since O(log N) is the best sorting complexity.

如果你正在寻找一种方法来排序元素,但也能够以一种有效的方式通过索引访问它们,你可以做以下事情:

使用随机访问列表进行存储(例如ArrayList) 确保它总是有序的

然后,要添加或删除元素,可以使用集合。binarySearch获取插入/删除索引。因为您的列表实现了随机访问,所以您可以使用确定的索引有效地修改列表。

例子:

/**
 * @deprecated
 *      Only for demonstration purposes. Implementation is incomplete and does not 
 *      handle invalid arguments.
 */
@Deprecated
public class SortingList<E extends Comparable<E>> {
    private ArrayList<E> delegate;
    
    public SortingList() {
        delegate = new ArrayList<>();
    }
    
    public void add(E e) {
        int insertionIndex = Collections.binarySearch(delegate, e);
        
        // < 0 if element is not in the list, see Collections.binarySearch
        if (insertionIndex < 0) {
            insertionIndex = -(insertionIndex + 1);
        }
        else {
            // Insertion index is index of existing element, to add new element 
            // behind it increase index
            insertionIndex++;
        }
        
        delegate.add(insertionIndex, e);
    }
    
    public void remove(E e) {
        int index = Collections.binarySearch(delegate, e);
        delegate.remove(index);
    }
    
    public E get(int index) {
        return delegate.get(index);
    }
}

(在这个答案中可以看到更完整的实现)

因为List的概念与自动排序集合的概念是不兼容的。List的意义在于调用List之后。添加(7,elem),调用list.get(7)将返回elem。在自动排序的列表中,元素可以位于任意位置。