Table of Contents
ToggleSorting List in Java with Examples
提供了建立自定義比較器,比較器是實作 Comparator 接口的物件,調用 Collections sort 、 List sort 、 Stream sorted 等方法,對集合進行排序, Java Sorting List with Examples 自定義比較器,比較兩個物件並傳回一個整數,本篇增加了範例,並透過單元測試來驗證產出結果。
檔案目錄
./
+- src
+- test
| +- org
| +- ruoxue
| +- java_147
| +- list
| +- sort
| +- SortingListWithExamplesTest.java
單元測試
Java Sorting List 提供不同類型的資料進行排序,使用自定義比較器,排序集合中的元素。
Fruit
建立 Fruit 類別,覆寫 toString ,定義屬性和方法,用來建立一個物件。
@NoArgsConstructor
@Getter
@Setter
@Builder
public static class Fruit {
private String name;
private double quantity;
private int type;
public Fruit(String name, double quantity, int type) {
this.name = name;
this.quantity = quantity;
this.type = type;
}
public String toString() {
ToStringBuilder builder = new ToStringBuilder(this, ToStringStyle.JSON_STYLE);
builder.appendSuper(super.toString());
builder.append("name", name);
builder.append("quantity", quantity);
builder.append("type", type);
return builder.toString();
}
}
Collections_sort
Java Sorting List 建立一個 List ,增加三個元素,使用 Collections sort 方法,依照自定義比較器,由小到大,升序對集合進行排序。
@Test
public void Collections_sort() {
Fruit mango = new Fruit("Mango", Double.MAX_VALUE, 1);
Fruit peach = new Fruit("Peach", 3, 1);
Fruit orange = new Fruit("Orange", -1, 3);
Arrays.asList(mango, peach, orange);
System.out.println(list);
Collections.sort(list, Comparator.comparing(Fruit::getType).thenComparing(Fruit::getQuantity));
System.out.println(list);
assertThat(list).containsExactly(peach, mango, orange);
}
[{"name":"Mango","quantity":1.7976931348623157E308,"type":1}, {"name":"Peach","quantity":3.0,"type":1}, {"name":"Orange","quantity":-1.0,"type":3}]
[{"name":"Peach","quantity":3.0,"type":1}, {"name":"Mango","quantity":1.7976931348623157E308,"type":1}, {"name":"Orange","quantity":-1.0,"type":3}]
Collections_sort_withMultipleConditions
Java Sorting List 建立一個 List ,增加三個元素,使用 Collections sort 方法,依照多個自定義比較器,由小到大,升序對集合進行排序。
protected static Comparator<Fruit> nameComparator = new Comparator<Fruit>() {
@Override
public int compare(Fruit o1, Fruit o2) {
return o1.name.compareTo(o2.name);
}
};
protected static Comparator<Fruit> quantityComparator = (o1, o2) -> Double.compare(o1.quantity, o2.quantity);
@Test
public void Collections_sort_withMultipleConditions() {
Fruit mango = new Fruit("Mango", Double.MAX_VALUE, 1);
Fruit peach = new Fruit("Peach", 3, 1);
Fruit orange = new Fruit("Orange", -1, 3);
Arrays.asList(mango, peach, orange);
System.out.println(list);
Collections.sort(list, nameComparator.thenComparing(quantityComparator));
System.out.println(list);
assertThat(list).containsExactly(mango, orange, peach);
}
[{"name":"Mango","quantity":1.7976931348623157E308,"type":1}, {"name":"Peach","quantity":3.0,"type":1}, {"name":"Orange","quantity":-1.0,"type":3}]
[{"name":"Mango","quantity":1.7976931348623157E308,"type":1}, {"name":"Orange","quantity":-1.0,"type":3}, {"name":"Peach","quantity":3.0,"type":1}]
List_sort
Sorting List Java 建立一個 List ,增加三個元素,使用 List sort 方法,依照自定義比較器,由小到大,升序對集合進行排序。
@Test
public void List_sort() {
Fruit apple = new Fruit("Apple", Double.MAX_VALUE, 1);
Fruit cherry = new Fruit("Cherry", 3, 1);
Fruit banana = new Fruit("Banana", -1, 3);
List<Fruit> list = Arrays.asList(apple, cherry, banana);
System.out.println(list);
list.sort(Comparator.comparing(Fruit::getType).thenComparing(Fruit::getQuantity));
System.out.println(list);
assertThat(list).containsExactly(cherry, apple, banana);
}
[{"name":"Apple","quantity":1.7976931348623157E308,"type":1}, {"name":"Cherry","quantity":3.0,"type":1}, {"name":"Banana","quantity":-1.0,"type":3}]
[{"name":"Cherry","quantity":3.0,"type":1}, {"name":"Apple","quantity":1.7976931348623157E308,"type":1}, {"name":"Banana","quantity":-1.0,"type":3}]
List_sort_withMultipleConditions
Sorting List Java 建立一個 List ,增加三個元素,使用 List sort 方法,依照多個自定義比較器,由小到大,升序對集合進行排序。
@Test
public void List_sort_withMultipleConditions() {
Fruit apple = new Fruit("Apple", Double.MAX_VALUE, 1);
Fruit cherry = new Fruit("Cherry", 3, 1);
Fruit banana = new Fruit("Banana", -1, 3);
List<Fruit> list = Arrays.asList(apple, cherry, banana);
System.out.println(list);
list.sort(nameComparator.thenComparing(quantityComparator));
System.out.println(list);
assertThat(list).containsExactly(apple, banana, cherry);
}
[{"name":"Apple","quantity":1.7976931348623157E308,"type":1}, {"name":"Cherry","quantity":3.0,"type":1}, {"name":"Banana","quantity":-1.0,"type":3}]
[{"name":"Apple","quantity":1.7976931348623157E308,"type":1}, {"name":"Banana","quantity":-1.0,"type":3}, {"name":"Cherry","quantity":3.0,"type":1}]
Stream_sorted
List Sort in Java 建立一個 List ,增加三個元素,使用 Stream sorted 方法,依照自定義比較器,由小到大,升序對集合進行排序。
@Test
public void Stream_sorted() {
Fruit lichee = new Fruit("Lichee", Double.MAX_VALUE, 1);
Fruit plum = new Fruit("Plum", 3, 1);
Fruit coconut = new Fruit("Coconut", -1, 3);
List<Fruit> list = Arrays.asList(lichee, plum, coconut);
System.out.println(list);
ArrayList<Fruit> result = list.stream()
.sorted(Comparator.comparing(Fruit::getType).thenComparing(Fruit::getQuantity))
.collect(Collectors.toCollection(ArrayList::new));
System.out.println(result);
assertThat(result).containsExactly(plum, lichee, coconut);
}
[{"name":"Lichee","quantity":1.7976931348623157E308,"type":1}, {"name":"Plum","quantity":3.0,"type":1}, {"name":"Coconut","quantity":-1.0,"type":3}]
[{"name":"Plum","quantity":3.0,"type":1}, {"name":"Lichee","quantity":1.7976931348623157E308,"type":1}, {"name":"Coconut","quantity":-1.0,"type":3}]
Stream_sorted_withMultipleConditions
List Sort in Java 建立一個 List ,增加三個元素,使用 Stream sorted 方法,依照多個自定義比較器,由小到大,升序對集合進行排序, Java Sorting List Example 提供範例參考。
@Test
public void Stream_sorted_withMultipleConditions() {
Fruit lichee = new Fruit("Lichee", Double.MAX_VALUE, 1);
Fruit plum = new Fruit("Plum", 3, 1);
Fruit coconut = new Fruit("Coconut", -1, 3);
List<Fruit> list = Arrays.asList(lichee, plum, coconut);
System.out.println(list);
ArrayList<Fruit> result = list.stream().sorted(nameComparator.thenComparing(quantityComparator))
.collect(Collectors.toCollection(ArrayList::new));
System.out.println(result);
assertThat(result).containsExactly(coconut, lichee, plum);
}
[{"name":"Lichee","quantity":1.7976931348623157E308,"type":1}, {"name":"Plum","quantity":3.0,"type":1}, {"name":"Coconut","quantity":-1.0,"type":3}]
[{"name":"Coconut","quantity":-1.0,"type":3}, {"name":"Lichee","quantity":1.7976931348623157E308,"type":1}, {"name":"Plum","quantity":3.0,"type":1}]
SortingListWithExamplesTest.java
Sorting List Java 新增單元測試,驗證 Java Sorting List Example 是否符合預期。
package org.ruoxue.java_147.list.sort;
import static org.assertj.core.api.Assertions.assertThat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
import org.junit.Test;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
public class SortingListWithExamplesTest {
@NoArgsConstructor
@Getter
@Setter
@Builder
public static class Fruit {
private String name;
private double quantity;
private int type;
public Fruit(String name, double quantity, int type) {
this.name = name;
this.quantity = quantity;
this.type = type;
}
public String toString() {
ToStringBuilder builder = new ToStringBuilder(this, ToStringStyle.JSON_STYLE);
builder.appendSuper(super.toString());
builder.append("name", name);
builder.append("quantity", quantity);
builder.append("type", type);
return builder.toString();
}
}
protected static Comparator<Fruit> nameComparator = new Comparator<Fruit>() {
@Override
public int compare(Fruit o1, Fruit o2) {
return o1.name.compareTo(o2.name);
}
};
protected static Comparator<Fruit> quantityComparator = (o1, o2) -> Double.compare(o1.quantity, o2.quantity);
@Test
public void Collections_sort() {
Fruit mango = new Fruit("Mango", Double.MAX_VALUE, 1);
Fruit peach = new Fruit("Peach", 3, 1);
Fruit orange = new Fruit("Orange", -1, 3);
List<Fruit> list = Arrays.asList(mango, peach, orange);
System.out.println(list);
Collections.sort(list, Comparator.comparing(Fruit::getType).thenComparing(Fruit::getQuantity));
System.out.println(list);
assertThat(list).containsExactly(peach, mango, orange);
}
@Test
public void Collections_sort_withMultipleConditions() {
Fruit mango = new Fruit("Mango", Double.MAX_VALUE, 1);
Fruit peach = new Fruit("Peach", 3, 1);
Fruit orange = new Fruit("Orange", -1, 3);
List<Fruit> list = Arrays.asList(mango, peach, orange);
System.out.println(list);
Collections.sort(list, nameComparator.thenComparing(quantityComparator));
System.out.println(list);
assertThat(list).containsExactly(mango, orange, peach);
}
@Test
public void List_sort() {
Fruit apple = new Fruit("Apple", Double.MAX_VALUE, 1);
Fruit cherry = new Fruit("Cherry", 3, 1);
Fruit banana = new Fruit("Banana", -1, 3);
List<Fruit> list = Arrays.asList(apple, cherry, banana);
System.out.println(list);
list.sort(Comparator.comparing(Fruit::getType).thenComparing(Fruit::getQuantity));
System.out.println(list);
assertThat(list).containsExactly(cherry, apple, banana);
}
@Test
public void List_sort_withMultipleConditions() {
Fruit apple = new Fruit("Apple", Double.MAX_VALUE, 1);
Fruit cherry = new Fruit("Cherry", 3, 1);
Fruit banana = new Fruit("Banana", -1, 3);
List<Fruit> list = Arrays.asList(apple, cherry, banana);
System.out.println(list);
list.sort(nameComparator.thenComparing(quantityComparator));
System.out.println(list);
assertThat(list).containsExactly(apple, banana, cherry);
}
@Test
public void Stream_sorted() {
Fruit lichee = new Fruit("Lichee", Double.MAX_VALUE, 1);
Fruit plum = new Fruit("Plum", 3, 1);
Fruit coconut = new Fruit("Coconut", -1, 3);
List<Fruit> list = Arrays.asList(lichee, plum, coconut);
System.out.println(list);
ArrayList<Fruit> result = list.stream()
.sorted(Comparator.comparing(Fruit::getType).thenComparing(Fruit::getQuantity))
.collect(Collectors.toCollection(ArrayList::new));
System.out.println(result);
assertThat(result).containsExactly(plum, lichee, coconut);
}
@Test
public void Stream_sorted_withMultipleConditions() {
Fruit lichee = new Fruit("Lichee", Double.MAX_VALUE, 1);
Fruit plum = new Fruit("Plum", 3, 1);
Fruit coconut = new Fruit("Coconut", -1, 3);
List<Fruit> list = Arrays.asList(lichee, plum, coconut);
System.out.println(list);
ArrayList<Fruit> result = list.stream().sorted(nameComparator.thenComparing(quantityComparator))
.collect(Collectors.toCollection(ArrayList::new));
System.out.println(result);
assertThat(result).containsExactly(coconut, lichee, plum);
}
}
心得分享
Java Sorting List Example 提供了幾種 Sort 常見方法的操作範例,對不同類型的資料進行排序,像是字串、數字和物件,對資料進行排序可能很有用,例如:產品可以根據用戶設定的要求,像是:價格、評級、品牌,以多種方式排序,熟悉 Sorting List Java 這些方法的操作,讓排序依照不同的需求,快速方便地實現功能。