为什么Java里面没有 SortedList

问题:为什么Java里面没有 SortedList

Java 里面有SortedSet和SortedMap接口,它们都属于Java的集合框架和提供对元素进行排序的方法

然鹅,在我的认知里Java就没有SortedList这个东西。你只能使用java.util.Collections.sort()去排序一个list

干嘛要这样设计鸭?

回答一

List 迭代器首要保证的是你获取的元素的顺序是按照list的内部顺序(或者插入顺序)。更加具体来说就是你插入元素的顺序或者按照你是怎么样操作这个list的。排序可以看成是对数据结构的一种操作,下面有几种方法去排序一个list

我会按照我认为的实用性的顺序去罗列这些方法

  1. 考虑使用 Set 或者 Bag 集合

我把这个放在第一位是因为这是你经常做的

sorted set可以自动地对插入的集合进行排序,意味着你插入元素进入集合里面的时候不用手动排序。

如果你确定你不用担心重复的元素的话,用TreeSet也行。它实现的是SortedSet 和 NavigableSet接口,就好像你期望的list那样工作的。

TreeSet<String> set = new TreeSet<String>();
set.add("lol");
set.add("cat");
// automatically sorts natural order when addingfor (String s : set) {System.out.println(s);
}
// Prints out "cat" and "lol"

如果你不想按自然顺序的话,你可以使用带Comparator参数的构造器

可供选择地,你也可以用Multisets(也叫Bags),这也是一个运行重复元素的set,它们是第一个第三方的实现。在Guava库里面有个叫TreeMultiset,工作机理很像TreeSet的。

  1. 用Collections.sort()排序你的list

正如上面说的那样,排序一个list是对数据结构的操作

你可以用java.util.Collections.sort() 方法排序一个list。下面是使用的代码实例:

List<String> strings = new ArrayList<String>()
strings.add("lol");
strings.add("cat");Collections.sort(strings);
for (String s : strings) {System.out.println(s);
}
// Prints out "cat" and "lol"

文章翻译自Stack Overflow:https://stackoverflow.com/questions/8725387/why-is-there-no-sortedlist-in-java

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/390971.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

图片主成分分析后的可视化_主成分分析-可视化

图片主成分分析后的可视化If you have ever taken an online course on Machine Learning, you must have come across Principal Component Analysis for dimensionality reduction, or in simple terms, for compression of data. Guess what, I had taken such courses too …

回溯算法和递归算法_回溯算法:递归和搜索示例说明

回溯算法和递归算法Examples where backtracking can be used to solve puzzles or problems include:回溯可用于解决难题或问题的示例包括&#xff1a; Puzzles such as eight queens puzzle, crosswords, verbal arithmetic, Sudoku [nb 1], and Peg Solitaire. 诸如八个皇后…

C#中的equals()和==

using System;namespace EqualsTest {class EqualsTest{static void Main(string[] args){//值类型int x 1;int y 1;Console.WriteLine(x y);//TrueConsole.WriteLine(x.Equals(y));//True //引用类型A a new A();B b new B();//Console.WriteLine(ab);//报错…

JPA JoinColumn vs mappedBy

问题&#xff1a;JPA JoinColumn vs mappedBy 两者的区别是什么呢 Entity public class Company {OneToMany(cascade CascadeType.ALL , fetch FetchType.LAZY)JoinColumn(name "companyIdRef", referencedColumnName "companyId")private List<B…

TP引用样式表和js文件及验证码

TP引用样式表和js文件及验证码 引入样式表和js文件 <script src"__PUBLIC__/bootstrap/js/jquery-1.11.2.min.js"></script> <script src"__PUBLIC__/bootstrap/js/bootstrap.min.js"></script> <link href"__PUBLIC__/bo…

pytorch深度学习_深度学习和PyTorch的推荐系统实施

pytorch深度学习The recommendation is a simple algorithm that works on the principle of data filtering. The algorithm finds a pattern between two users and recommends or provides additional relevant information to a user in choosing a product or services.该…

什么是JavaScript中的回调函数?

This article gives a brief introduction to the concept and usage of callback functions in the JavaScript programming language.本文简要介绍了JavaScript编程语言中的回调函数的概念和用法。 函数就是对象 (Functions are Objects) The first thing we need to know i…

Java 集合-集合介绍

2017-10-30 00:01:09 一、Java集合的类关系图 二、集合类的概述 集合类出现的原因&#xff1a;面向对象语言对事物的体现都是以对象的形式&#xff0c;所以为了方便对多个对象的操作&#xff0c;Java就提供了集合类。数组和集合类同是容器&#xff0c;有什么不同&#xff1a;数…

为什么Java不允许super.super.method();

问题&#xff1a;为什么Java不允许super.super.method(); 我想出了这个问题&#xff0c;认为这个是很好解决的&#xff08;也不是没有它就不行的&#xff09;如果可以像下面那样写的话&#xff1a; Override public String toString() {return super.super.toString(); }我不…

Exchange 2016部署实施案例篇-04.Ex基础配置篇(下)

上二篇我们对全新部署完成的Exchange Server做了基础的一些配置&#xff0c;今天继续基础配置这个话题。 DAG配置 先决条件 首先在配置DGA之前我们需要确保DAG成员服务器上磁盘的盘符都是一样的&#xff0c;大小建议最好也相同。 其次我们需要确保有一块网卡用于数据复制使用&…

数据库课程设计结论_结论:

数据库课程设计结论In this article, we will learn about different types[Z Test and t Test] of commonly used Hypothesis Testing.在本文中&#xff0c;我们将学习常用假设检验的不同类型[ Z检验和t检验 ]。 假设是什么&#xff1f; (What is Hypothesis?) This is a St…

JavaScript数据类型:Typeof解释

typeof is a JavaScript keyword that will return the type of a variable when you call it. You can use this to validate function parameters or check if variables are defined. There are other uses as well.typeof是一个JavaScript关键字&#xff0c;当您调用它时将…

asp.net读取用户控件,自定义加载用户控件

1、自定义加载用户控件 ceshi.aspx页面 <html><body> <div id"divControls" runat"server"></div> </body></html> ceshi.aspx.cs页面 System.Web.UI.UserControl newUC (System.Web.UI.UserControl)Page.LoadContro…

配置Java_Home,临时环境变量信息

一、内容回顾 上一篇博客《Java运行环境的搭建---Windows系统》 我们说到了配置path环境变量的目的在于控制台可以在任意路径下都可以找到java的开发工具。 二、配置其他环境变量 1. 原因 为了获取更大的用户群体&#xff0c;所以使用java语言开发系统需要兼容不同版本的jdk&a…

网页缩放与窗口缩放_功能缩放—不同的Scikit-Learn缩放器的效果:深入研究

网页缩放与窗口缩放内部AI (Inside AI) In supervised machine learning, we calculate the value of the output variable by supplying input variable values to an algorithm. Machine learning algorithm relates the input and output variable with a mathematical func…

在构造器里调用可重写的方法有什么问题?

问题&#xff1a;在构造器里调用可重写的方法有什么问题&#xff1f; 我有一个检票页面的类通过抽象方法的结果去去设置页的标题 public abstract class BasicPage extends WebPage {public BasicPage() {add(new Label("title", getTitle()));}protected abstract…

创建hugo博客_如何创建您的第一个Hugo博客:实用指南

创建hugo博客Hugo is a great tool to use if you want to start a blog.如果您想创建博客&#xff0c;Hugo是一个很好的工具。 I use Hugo myself for my blog, flaviocopes.com, and Ive been using it for more than two years. I have a few reasons for loving Hugo.我本…

Python自动化开发01

一、 变量变量命名规则变量名只能是字母、数字或下划线的任意组合变量名的第一个字符不能是数字以下关键字不能声明为变量名 [and, as, assert, break, class, continue, def, del, elif, else, except, exec, finally, for, from, global, if, import, in, is, lambda, not,…

记录关于vs2008 和vs2015 的报错问题

出现了 VS2008无法创建项目&#xff0c;无法打开项目的情况&#xff0c;提示这个注册表键值有问题 HKEY_LOCAL_MACHINE \ SOFTWARE \ Microsoft \ MSBuild \ ToolsVersions \ 14.0&#xff0c; 但是查看了注册表没有这个键。费尽辛万苦&#xff0c;中午在思密达的一个网站上看到…

未越狱设备提取数据_从三星设备中提取健康数据

未越狱设备提取数据Health data is collected every time you have your phone in your pocket. Apple or Android, the phones are equipped with a pedometer that counts your steps. Hence, health data is recorded. This data could be your one free data mart for a si…