博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
java对列表数据排序_如何在Java中对列表进行排序
阅读量:2530 次
发布时间:2019-05-11

本文共 3817 字,大约阅读时间需要 12 分钟。

java对列表数据排序

Sometimes we have to sort a list in Java before processing its elements. In this tutorial, we will learn how to sort a list in the natural order. We will also learn how to use our own Comparator implementation to sort a list of objects.

有时,我们必须在Java中对列表进行排序,然后再处理其元素。 在本教程中,我们将学习如何以自然顺序对列表进行排序。 我们还将学习如何使用我们自己的Comparator实现对对象列表进行排序。

Java List is similar to arrays except that the length of the list is dynamic and it comes in . Actually, is an interface and most of the time we use one of its implementation like or etc.

Java List与数组相似,不同之处在于列表的长度是动态的,并且在 。 实际上, 是一个接口,大多数时候我们使用其实现之一,例如或等。

Java排序列表 (Java Sort List)

Here we will learn how to sort a list of Objects in Java. We can use Collections.sort() method to sort a list in the natural ascending order. All the elements in the list must implement interface, otherwise IllegalArgumentException is thrown.

在这里,我们将学习如何对Java中的对象列表进行排序。 我们可以使用Collections.sort()方法以自然的升序对列表进行排序。 列表中的所有元素必须实现接口,否则将抛出IllegalArgumentException

Let’s look at a quick example to sort a list of strings.

让我们看一个简单的示例,以对字符串列表进行排序。

package com.journaldev.sort;import java.util.ArrayList;import java.util.Collections;import java.util.List;public class JavaListSort {    /**     * This class shows how to sort ArrayList in java     * @param args     */    public static void main(String[] args) {        List
strList = new ArrayList
(); strList.add("A"); strList.add("C"); strList.add("B"); strList.add("Z"); strList.add("E"); //using Collections.sort() to sort ArrayList Collections.sort(strList); for(String str: strList) System.out.print(" "+str); }}

As you can see that we are using Collections.sort() method to sort the list of Strings. The String class implements Comparable interface.

如您所见,我们正在使用Collections.sort()方法对字符串列表进行排序。 String类实现Comparable接口。

Output:

输出:

Java Sort List

Java Sort List

Java排序列表

Java对象排序列表 (Java Sort List of Objects)

Let’s see another example where we will sort a list of custom objects. Note that the class must implement Comparable interface.

让我们看另一个示例,在该示例中我们将对自定义对象列表进行排序。 请注意,该类必须实现Comparable接口。

package com.journaldev.sort;import java.util.ArrayList;import java.util.Collections;import java.util.List;public class JavaSortListObject {	public static void main(String[] args) {		List dl = new ArrayList<>();		dl.add(new Data(2));		dl.add(new Data(3));		dl.add(new Data(1));		System.out.println("Original List::"+dl);		Collections.sort(dl);		System.out.println("Naturally Sorted List::"+dl);	}}class Data implements Comparable {	private int id;	public Data(int i) {		this.id = i;	}	@Override	public int compareTo(Data d) {		return this.id - d.getId();	}	public int getId() {		return id;	}	@Override	public String toString() {		return "Data{"+this.id+"}";	}}

Output:

输出:

Original List::[Data{2}, Data{3}, Data{1}]Naturally Sorted List::[Data{1}, Data{2}, Data{3}]

使用Comparator在Java中对列表进行排序 (Sort a List in Java using Comparator)

Collections.sort() method is overloaded and we can also provide our own implementation for sorting rules.

Collections.sort()方法已重载,我们还可以提供自己的实现来进行排序规则。

Since Comparator is a , we can use to write its implementation in a single line.

由于Comparator是一个 ,因此我们可以使用在一行中编写其实现。

Collections.sort(dl, (d1, d2) -> {	return d2.getId() - d1.getId();});System.out.println("Reverse Sorted List using Comparator::" + dl);

Output:

输出:

Java Sort List Objects Comparator

Java Sort List Objects – Comparator

Java排序列表对象–比较器

摘要 (Summary)

class sort() method is used to sort a list in Java. We can sort a list in natural ordering where the list elements must implement Comparable interface. We can also pass a Comparator implementation to define the sorting rules.

类的sort()方法用于对Java中的列表进行排序。 我们可以按照自然顺序对列表进行排序,其中list元素必须实现Comparable接口。 我们还可以通过Comparator实现来定义排序规则。

. 查看更多示例。

Reference:

参考:

翻译自:

java对列表数据排序

转载地址:http://amozd.baihongyu.com/

你可能感兴趣的文章
Ubuntu 18.04 root 使用ssh密钥远程登陆
查看>>
Servlet和JSP的异同。
查看>>
虚拟机centOs Linux与Windows之间的文件传输
查看>>
ethereum(以太坊)(二)--合约中属性和行为的访问权限
查看>>
IOS内存管理
查看>>
middle
查看>>
[Bzoj1009][HNOI2008]GT考试(动态规划)
查看>>
Blob(二进制)、byte[]、long、date之间的类型转换
查看>>
OO第一次总结博客
查看>>
day7
查看>>
iphone移动端踩坑
查看>>
vs无法加载项目
查看>>
Beanutils基本用法
查看>>
玉伯的一道课后题题解(关于 IEEE 754 双精度浮点型精度损失)
查看>>
《BI那点儿事》数据流转换——百分比抽样、行抽样
查看>>
哈希(1) hash的基本知识回顾
查看>>
Leetcode 6——ZigZag Conversion
查看>>
dockerfile_nginx+PHP+mongo数据库_完美搭建
查看>>
Http协议的学习
查看>>
【转】轻松记住大端小端的含义(附对大端和小端的解释)
查看>>