java对列表数据排序_如何在Java中对列表进行排序

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 Java Collection framework. Actually, List is an interface and most of the time we use one of its implementation like ArrayList or LinkedList etc.

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

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 Comparable interface, otherwise IllegalArgumentException is thrown.

在这里,我们将学习如何对Java中的对象列表进行排序。 我们可以使用Collections.sort()方法以自然的升序对列表进行排序。 列表中的所有元素必须实现Comparable接口,否则将抛出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<String> strList = new ArrayList<String>();
        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<Data> 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<Data> {

	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 Comparator implementation for sorting rules.

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

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

由于Comparator是一个功能接口 ,因此我们可以使用lambda表达式在一行中编写其实现。

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)

Collections 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实现来定义排序规则。

GitHub Repository. GitHub存储库中查看更多示例。

Reference: API Doc

参考: API文档

翻译自: https://www.journaldev.com/787/java-sort-list

java对列表数据排序

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值