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

In Java there are the SortedSet and SortedMap interfaces. 在Java中,有SortedSet和SortedMap接口。 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. 树数据结构SortedSet和SortedMap接口分别使用使用的Red-Black树实现算法实现TreeSet和TreeMap 。 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. 它根据元素的自然顺序将指定列表按升序排序。

java sortedlist 用法_为什么Java中没有SortedList?相关推荐

  1. java callable用法_在Java中使用Callable和Future

    最近,我们正在开发一个监控仪表板,我们想要ping几个第三方服务器,只是为了检查它们的可用性,或者我们想要调用健康检查. 我们正在对服务器进行一系列api调用,这些服务器的IP,用户名和密码被外部化为 ...

  2. java expression 用法_浅析Java 8新特性Lambda Expression

    什么是Lambda Expression 对于Lambda Expression,我的理解是,它是一个函数表达式,如下: (int x, int y) -> x - y 符号左边定义了函数的输入 ...

  3. java future用法_纯干货:Java学习过程中的21个知识点和技术点

    我们在Java学习过程中要学会抓重点,善于总结,Java学习过程中常见的21个知识点和技术点你知道吗?下面和千锋广州小编一起来看看吧! 1. JVM相关 对于刚刚接触Java的人来说,JVM相关的知识 ...

  4. java path类_基于java Files类和Paths类的用法(详解)

    Java7中文件IO发生了很大的变化,专门引入了很多新的类: import java.nio.file.DirectoryStream; import java.nio.file.FileSystem ...

  5. java final 类_在Java中,final修饰的类有什么特点

    展开全部 关于Java中的32313133353236313431303231363533e4b893e5b19e31333264663736final(2010-09-09 14:19:48)转载▼ ...

  6. java coin介绍_代码示例中的Java 7:Project Coin

    java coin介绍 该博客通过代码示例介绍了一些新的Java 7功能,这些项目在Project Coin一词下进行了概述. Project Coin的目标是向JDK 7添加一组小的语言更改.这些更 ...

  7. java skip函数_【Java必修课】图说Stream中的skip()和limit()方法及组合使用

    1 简介 本文将讲解Java 8 Stream中的两个方法:skip()和limit().这两个方法是Stream很常用的,不仅各自会被高频使用,还可以组合出现,并能实现一些小功能,如subList和 ...

  8. java 栈的用法_让Java程序员再次生机勃勃,还是技术的力量

    大多数人选择Java可能只是因为听说Java前景好.Java比较好找工作.Java语言在TIOBE排行榜上一直位于前三等等之类的原因,但是Java真的还是之前那么好吗?答案是肯定的. 下面给大家分享一 ...

  9. java future用法_你必须掌握的 21 个 Java 核心技术

    作者:工程师-搁浅来源:https://www.jb51.net/article/122070.htm 写这篇文章的目的是想总结一下自己这么多年来使用java的一些心得体会,主要是和一些java基础知 ...

最新文章

  1. 电子书下载:Building Websites with DotNetNuke 5
  2. 新年到了,该规划一下来年了
  3. oracle日期相减工作日_Oracle 计算两个日期间隔的天数、月数和年数
  4. 作者:杨燕(1964-),女,西南交通大学信息科学与技术学院教授、博士生导师。...
  5. 【IDEA】Error:java: Compilation failed: internal java compiler error
  6. 【备份】golang开发环境搭建
  7. 逆向分析CrackMe系列——CrackMe001
  8. 小龙虾,这个“三流入侵者”竟成“钻石网红”?
  9. c语言线性表写的数据库系统(数据结构)
  10. (P24)管道:管道的读写规则
  11. 基于Python实现数字图像可视化水印系统
  12. 微信小程序云开发-树洞小程序Treehole(评论帖子/发布话题实现)
  13. 五款功能强大的国产软件,常常被误认为是外国人开发的
  14. 关于CCleaner 卸载程序删除所有软件文件的现象
  15. C语言:输出1到100中3的倍数
  16. python界面编程 plc_Python实现PLC数据可视化呈现于Web端
  17. 易语言大漠找字FindStrE系列
  18. 2023 红明谷杯 --- Crypto It Takes Two! wp
  19. Chrome插件之批量获取淘宝视频URL工具
  20. 宋立波:闲谈运营商手机软件商城和应用工厂(运营商和手机开发者请看来)...

热门文章

  1. tar解压到制定目录
  2. 黑马大赛:2013年度十大创业家
  3. Python使用yaml配置logging日志
  4. 苹果光盘刻录实用技巧指南(转)
  5. 基本计数、排列组合与Lucas定理
  6. golang——defer
  7. @Documented,@Retention,@Target
  8. 东芝发布OCZ VX500系列SATA SSD新品:采用15nm MLC闪存
  9. 寿险业务系统异常处理方案
  10. android之城市定位