本文共 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与数组相似,不同之处在于列表的长度是动态的,并且在 。 实际上, 是一个接口,大多数时候我们使用其实现之一,例如或等。
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) { ListstrList = 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:
输出:
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}]
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:
输出:
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/