为什么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. 它根据元素的自然顺序将指定列表按升序排序。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值