今天我们将研究Java Collections的分类方法,在使用Java Collections(/community/tutorials/collections-in-java-tutorial)时,我们需要更频繁地对数据进行分类。
Java 收藏品 sort()
Java Collections 类提供了一种非常方便的方法,即Collections.sort()
来排序所有 List的实现,例如 LinkedList和 ArrayList。
sort(列表列表)
:以自然排序的上升顺序对列表中的元素进行排序.sort(列表列表,比较器c)
:按照比较器引发的顺序对列表中的元素进行排序(/community/tutorials/comparable-and-comparator-in-java-example)。
请注意,上述方法签名使用( / 社区 / 教程 / java-generics - 例子 - 方法 - 类 - 接口),但我已经在这里删除它们来简化阅读。
Java 收藏类型(列表列表)
考虑一个String
的ArrayList
:
1List<String> fruits = new ArrayList<String>();
2fruits.add("Apple");
3fruits.add("Orange");
4fruits.add("Banana");
5fruits.add("Grape");
现在,我们将使用Collections.sort()
来排序它:
1Collections.sort(fruits);
2// Print the sorted list
3System.out.println(fruits);
该计划的结果将是:
1[Apple, Banana, Grape, Orange]
因此,我们可以看到,‘Collections.sort()’ 将字符串列表排序为 Lexical 顺序,并且不会返回任何东西。如果我们有自定义对象列表的话呢?当然,我们也可以排序它们。
1package com.journaldev.collections;
2public class Fruit{
3 private int id;
4 private String name;
5 private String taste;
6
7 Fruit(int id, String name, String taste){
8 this.id=id;
9 this.name=name;
10 this.taste=taste;
11 }
12}
让我们来做一份水果清单:
1List<Fruit> fruitList=new ArrayList<Fruit>();
2Fruit apple=new Fruit(1, "Apple", "Sweet");
3Fruit orange=new Fruit(2, "Orange", "Sour");
4Fruit banana=new Fruit(4, "Banana", "Sweet");
5Fruit grape=new Fruit(3, "Grape", "Sweet and Sour");
6
7fruitList.add(apple);
8fruitList.add(orange);
9fruitList.add(banana);
10fruitList.add(grape);
In order to sort this list, if we directly use the Collections.sort(List list)
, it will give a Compile Time Error because there is no natural ordering defined for the Fruit objects. So, it doesn't know how to sort this list. For objects to have a natural order they must implement the interface
java.lang.Comparable
. The Comparable
interface has a method compareTo()
, which returns a negative, 0, a positive if the current value is less than, equal to, or greater than the value we are comparing with, respectively. Let's enhance the Fruit class to implement Comparable
interface. We are defining that the natural order of sorting is based on the "id" field of Fruit:
1package com.journaldev.collections;
2public class Fruit implements Comparable<Object>{
3 private int id;
4 private String name;
5 private String taste;
6
7 Fruit(int id, String name, String taste){
8 this.id=id;
9 this.name=name;
10 this.taste=taste;
11 }
12 @Override
13 public int compareTo(Object o) {
14 Fruit f = (Fruit) o;
15 return this.id - f.id ;
16 }
17}
现在我们已经实现了比较
,我们可以排序列表没有任何错误:
1Collections.sort(fruitList);
2fruitList.forEach(fruit -> {
3 System.out.println(fruit.getId() + " " + fruit.getName() + " " +
4 fruit.getTaste());
5});
结果将如下:
11 Apple Sweet
22 Orange Sour
33 Grape Sweet and Sour
44 Banana Sweet
Java 收藏类型(列表列表,比较器 c)
为了定义一个自定义的排序逻辑,这与元素的自然排序不同,我们可以实施java.util.Comparator
界面,并将其中的一个实例传递为sort()
的第二个参数。
1package com.journaldev.collections;
2
3class SortByName implements Comparator<Fruit> {
4 @Override
5 public int compare(Fruit a, Fruit b) {
6 return a.getName().compareTo(b.getName());
7 }
8}
现在,我们可以使用这个比较器来排序它:
1Collections.sort(fruitList, new SortByName());
结果将如下:
11 Apple Sweet
24 Banana Sweet
33 Grape Sweet and Sour
42 Orange Sour
使用 lambda 函数而不是为 Comparator 编写新类,我们也可以在运行时提供分类逻辑:
1Collections.sort(fruitList, (a, b) -> {
2 return a.getName().compareTo(b.getName());
3});
Java 收藏.reverseOrder
默认情况下,‘Collection.sort’会以上升顺序进行排序,如果我们想要以反向顺序排序元素,我们可以使用以下方法:
reverseOrder()
:返回强制对集合元素的自然排序逆转的 `比较’。
以下是这两种方法的例子:
Java Collections reverseOrder() 示例
1Collections.sort(fruits, Collections.reverseOrder());
2System.out.println(fruits);
它将以相反的字母顺序输出水果:
1[Orange, Grape, Banana, Apple]
Java 收藏 reverseOrder(Comparator cmp) 示例
1Collections.sort(fruitList, Collections.reverseOrder(new SortByName()));
2fruitList.forEach(fruit -> {
3 System.out.println(fruit.getId() + " " + fruit.getName() + " " +
4 fruit.getTaste());
5});
输出:
12 Orange Sour
23 Grape Sweet and Sour
34 Banana Sweet
41 Apple Sweet
这就是Java Collections sort()方法的全部,它是例子。 参考: API Doc