爲什麼Java中沒有SortedList?

本文翻譯自:Why is there no SortedList in Java?

In Java there are the SortedSet and SortedMap interfaces. 在Java中,有SortedSetSortedMap接口。 Both belong to Java's standard Collections framework and provide a sorted way to access the elements. 兩者都屬於Java的標準集合框架,並提供了一種訪問元素的排序方式。

However, in my understanding there is no SortedList in Java. 但是,根據我的理解,Java中沒有SortedList You can use java.util.Collections.sort() to sort a list. 您可以使用java.util.Collections.sort()對列表進行排序。

Any idea why it is designed like that? 知道爲什麼它的設計是這樣的嗎?


#1樓

參考:https://stackoom.com/question/abs3/爲什麼Java中沒有SortedList


#2樓

First line in the List API says it is an ordered collection (also known as a sequence). List API中的第一行表示它是一個有序集合(也稱爲序列)。 If you sort the list you can't maintain the order, so there is no TreeList in Java. 如果對列表進行排序,則無法維護順序,因此Java中沒有TreeList。
As API says Java List got inspired from Sequence and see the sequence properties http://en.wikipedia.org/wiki/Sequence_(mathematics ) 正如API所說,Java List受到了Sequence的啓發,並且看到了序列屬性http://en.wikipedia.org/wiki/Sequence_(mathematics

It doesn't mean that you can't sort the list, but Java strict to his definition and doesn't provide sorted versions of lists by default. 這並不意味着您不能對列表進行排序,而是Java嚴格按照他的定義,並且默認情況下不提供列表的排序版本。


#3樓

Consider using indexed-tree-map . 考慮使用索引樹映射 It's an enhanced JDK's TreeSet that provides access to element by index and finding the index of an element without iteration or hidden underlying lists that back up the tree. 它是一個增強的JDK TreeSet,它通過索引提供對元素的訪問,並查找沒有迭代的元素索引或者備份樹的隱藏底層列表。 The algorithm is based on updating weights of changing nodes every time there is a change. 該算法基於每次改變時更新節點的權重。


#4樓

JavaFX SortedList JavaFX SortedList

Though it took a while, Java 8 does have a sorted List . 雖然花了一段時間,但Java 8確實有一個排序List http://docs.oracle.com/javase/8/javafx/api/javafx/collections/transformation/SortedList.html http://docs.oracle.com/javase/8/javafx/api/javafx/collections/transformation/SortedList.html

As you can see in the javadocs, it is part of the JavaFX collections, intended to provide a sorted view on an ObservableList. 正如您在javadocs中看到的,它是JavaFX集合的一部分,旨在提供ObservableList的排序視圖。

Update: Note that with Java 11, the JavaFX toolkit has moved outside the JDK and is now a separate library. 更新:請注意,對於Java 11,JavaFX工具包已移出JDK,現在是一個單獨的庫。 JavaFX 11 is available as a downloadable SDK or from MavenCentral. JavaFX 11可作爲可下載的SDK或MavenCentral提供。 See https://openjfx.io 請參閱https://openjfx.io


#5樓

For any newcomers, as of April 2015, Android now has a SortedList class in the support library, designed specifically to work with RecyclerView . 對於任何新手,截至2015年4月,Android現在在支持庫中有一個SortedList類,專門設計用於與RecyclerView Here's the blog post about it. 這是關於它的博客文章


#6樓

Set and Map are non-linear data structure. Set和Map是非線性數據結構。 List is linear data structure. 列表是線性數據結構。

在此輸入圖像描述


The tree data structure SortedSet and SortedMap interfaces implements TreeSet and TreeMap respectively using used Red-Black tree implementation algorithm. 樹數據結構SortedSetSortedMap接口分別使用使用的Red-Black樹實現算法實現TreeSetTreeMap So it ensure that there are no duplicated items (or keys in case of Map ). 因此,它確保沒有重複的項目(或Map情況下的鍵)。

  • List is already maintains an ordered collection and index-based data structure, trees are no index-based data structures. List已經維護了一個有序的集合和基於索引的數據結構,樹不是基於索引的數據結構。
  • Tree by definition cannot contain duplicates. 根據定義, Tree不能包含重複項。
  • In List we can have duplicates, so there is no TreeList (ie no SortedList ). List我們可以有重複項,因此沒有TreeList (即沒有SortedList )。
  • List maintains elements in insertion order. List按插入順序維護元素。 So if we want to sort the list we have to use java.util.Collections.sort() . 因此,如果我們要對列表進行排序,我們必須使用java.util.Collections.sort() It sorts the specified list into ascending order, according to the natural ordering of its elements. 它根據元素的自然順序將指定列表按升序排序。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章