Table of Contents
ToggleCollections sort in Java with Examples
提供了建立自定義比較器,比較器是實作 Comparator 接口的物件,調用 Collections sort 方法,對集合進行排序, Java Collections Sort with Examples 自定義比較器,比較兩個物件並傳回一個整數,本篇增加了範例,並透過單元測試來驗證產出結果。
public static <T extends Comparable<? super T>> void sort(List<T> list) {
}
public static <T> void sort(List<T> list, Comparator<? super T> c) {
}
檔案目錄
./
+- src
+- test
| +- org
| +- ruoxue
| +- java_147
| +- collection
| +- sort
| +- CollectionsSortWithExamplesTest.java
單元測試
Java Collections Sort 提供不同類型的資料進行排序,使用自定義比較器,排序集合中的元素。
sort
Java Collections Sort 建立一個 List ,增加三個元素,使用 Collections sort 方法,依照預設比較器,由小到大,升序對集合進行排序。
@Test
public void sort() {
List<Integer> list = Lists.newArrayList(Integer.MAX_VALUE, -1, 3);
System.out.println(list);
Collections.sort(list);
System.out.println(list);
assertThat(list).containsExactly(-1, 3, Integer.MAX_VALUE);
}
[2147483647, -1, 3]
[-1, 3, 2147483647]
sortWithReverseOrder
Java Collections Sort 建立一個 List ,增加三個元素,使用 Collections sort 方法,依照預設反向比較器,由小到大,升序對集合進行排序。
@Test
public void sortWithReverseOrder() {
List<Integer> list = Lists.newArrayList(Integer.MAX_VALUE, -1, 3);
System.out.println(list);
Collections.sort(list, Comparator.reverseOrder());
System.out.println(list);
assertThat(list).containsExactly(Integer.MAX_VALUE, 3, -1);
}
2147483647, -1, 3]
[2147483647, 3, -1]
sortWithComparator
Java Collections Sort 建立一個 List ,增加三個元素,使用 Collections sort 方法,依照自定義比較器,由小到大,升序對集合進行排序。
@Test
public void sortWithComparator() {
List<Integer> list = Lists.newArrayList(Integer.MAX_VALUE, -1, 3);
System.out.println(list);
Collections.sort(list, (i1, i2) -> Integer.compare(i1, i2));
System.out.println(list);
assertThat(list).containsExactly(-1, 3, Integer.MAX_VALUE);
}
[2147483647, -1, 3]
[-1, 3, 2147483647]
sortWithComparing
Collections Sort Java 建立一個 List ,增加三個元素,使用 Collections sort 方法,依照自定義比較器,由小到大,升序對集合進行排序。
@Test
public void sortWithComparing() {
List<Integer> list = Lists.newArrayList(Integer.MAX_VALUE, -1, 3);
System.out.println(list);
Collections.sort(list, Comparator.comparing(Integer::intValue));
System.out.println(list);
assertThat(list).containsExactly(-1, 3, Integer.MAX_VALUE);
}
[2147483647, -1, 3]
[-1, 3, 2147483647]
sortWithComparingInt
Collections Sort Java 建立一個 List ,增加三個元素,使用 Collections sort 方法,依照自定義比較器,由小到大,升序對集合進行排序。
@Test
public void sortWithComparingInt() {
List<Integer> list = Lists.newArrayList(Integer.MAX_VALUE, -1, 3);
System.out.println(list);
Collections.sort(list, Comparator.comparingInt(i -> i));
System.out.println(list);
assertThat(list).containsExactly(-1, 3, Integer.MAX_VALUE);
}
[2147483647, -1, 3]
[-1, 3, 2147483647]
sortWithMultipleConditions
Collections Sort Java 建立一個 List ,增加三個元素,使用 Collections sort 方法,依照多個自定義比較器,由小到大,升序對集合進行排序。
protected static Comparator<Integer> valueComparator = new Comparator<Integer>() {
@Override
public int compare(Integer i1, Integer i2) {
return Integer.compare(i1, i2);
}
};
protected static Comparator<Integer> lengthComparator = (i1, i2) -> Integer.compare(String.valueOf(i1).length(),
String.valueOf(i2).length());
@Test
public void sortWithMultipleConditions() {
List<Integer> list = Lists.newArrayList(Integer.MAX_VALUE, -1, 3);
System.out.println(list);
Collections.sort(list, valueComparator.thenComparing(lengthComparator));
System.out.println(list);
assertThat(list).containsExactly(-1, 3, Integer.MAX_VALUE);
}
[2147483647, -1, 3]
[-1, 3, 2147483647]
sortWithNull
建立一個 List ,增加三個元素及一個 null 元素,使用 Collections sort 方法,依照自定義比較器, null 元素放在第一個,由小到大,升序對集合進行排序。
@Test
public void sortWithNull() {
List<Integer> list = Lists.newArrayList(Integer.MAX_VALUE, -1, 3, null);
System.out.println(list);
Collections.sort(list, (s1, s2) -> {
if (s1 == null) {
return s2 == null ? 0 : -1;
} else if (s2 == null) {
return 1;
}
return s1.compareTo(s2);
});
System.out.println(list);
assertThat(list).containsExactly(null, -1, 3, Integer.MAX_VALUE);
}
[2147483647, -1, 3, null]
[null, -1, 3, 2147483647]
sortWithNullsFirst
建立一個 List ,增加三個元素及一個 null 元素,使用 Collections sort 方法,依照 nullsFirst 比較器, null 元素放在第一個,由小到大,升序對集合進行排序。
@Test
public void sortWithNullsFirst() {
List<Integer> list = Lists.newArrayList(Integer.MAX_VALUE, -1, 3, null);
System.out.println(list);
Collections.sort(list, Comparator.nullsFirst(Comparator.comparing(s -> s)));
System.out.println(list);
assertThat(list).containsExactly(null, -1, 3, Integer.MAX_VALUE);
}
[2147483647, -1, 3, null]
[null, -1, 3, 2147483647]
sortWithNullsLast
建立一個 List ,增加三個元素及一個 null 元素,使用 Collections sort 方法,依照 nullsLast 比較器, null 元素放在最後個,由小到大,升序對集合進行排序。
@Test
public void sortWithNullsLast() {
List<Integer> list = Lists.newArrayList(Integer.MAX_VALUE, -1, 3, null);
System.out.println(list);
Collections.sort(list, Comparator.nullsLast(Comparator.comparing(s -> s)));
System.out.println(list);
assertThat(list).containsExactly(-1, 3, Integer.MAX_VALUE, null);
}
[2147483647, -1, 3, null]
[-1, 3, 2147483647, null]
CollectionsSortWithExamplesTest.java
Collections Sort Java 新增單元測試,驗證 Java Collections Sort Example 是否符合預期。
package org.ruoxue.java_147.collection.list.sort;
import static org.assertj.core.api.Assertions.assertThat;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import org.junit.Test;
import com.google.common.collect.Lists;
public class CollectionsSortWithExamplesTest {
@Test
public void sort() {
List<Integer> list = Lists.newArrayList(Integer.MAX_VALUE, -1, 3);
System.out.println(list);
Collections.sort(list);
System.out.println(list);
assertThat(list).containsExactly(-1, 3, Integer.MAX_VALUE);
}
@Test
public void sortWithReverseOrder() {
List<Integer> list = Lists.newArrayList(Integer.MAX_VALUE, -1, 3);
System.out.println(list);
Collections.sort(list, Comparator.reverseOrder());
System.out.println(list);
assertThat(list).containsExactly(Integer.MAX_VALUE, 3, -1);
}
@Test
public void sortWithComparator() {
List<Integer> list = Lists.newArrayList(Integer.MAX_VALUE, -1, 3);
System.out.println(list);
Collections.sort(list, (i1, i2) -> Integer.compare(i1, i2));
System.out.println(list);
assertThat(list).containsExactly(-1, 3, Integer.MAX_VALUE);
}
@Test
public void sortWithComparing() {
List<Integer> list = Lists.newArrayList(Integer.MAX_VALUE, -1, 3);
System.out.println(list);
Collections.sort(list, Comparator.comparing(Integer::intValue));
System.out.println(list);
assertThat(list).containsExactly(-1, 3, Integer.MAX_VALUE);
}
@Test
public void sortWithComparingInt() {
List<Integer> list = Lists.newArrayList(Integer.MAX_VALUE, -1, 3);
System.out.println(list);
Collections.sort(list, Comparator.comparingInt(i -> i));
System.out.println(list);
assertThat(list).containsExactly(-1, 3, Integer.MAX_VALUE);
}
protected static Comparator<Integer> valueComparator = new Comparator<Integer>() {
@Override
public int compare(Integer i1, Integer i2) {
return Integer.compare(i1, i2);
}
};
protected static Comparator<Integer> lengthComparator = (i1, i2) -> Integer.compare(String.valueOf(i1).length(),
String.valueOf(i2).length());
@Test
public void sortWithMultipleConditions() {
List<Integer> list = Lists.newArrayList(Integer.MAX_VALUE, -1, 3);
System.out.println(list);
Collections.sort(list, valueComparator.thenComparing(lengthComparator));
System.out.println(list);
assertThat(list).containsExactly(-1, 3, Integer.MAX_VALUE);
}
@Test
public void sortWithNull() {
List<Integer> list = Lists.newArrayList(Integer.MAX_VALUE, -1, 3, null);
System.out.println(list);
Collections.sort(list, (s1, s2) -> {
if (s1 == null) {
return s2 == null ? 0 : -1;
} else if (s2 == null) {
return 1;
}
return s1.compareTo(s2);
});
System.out.println(list);
assertThat(list).containsExactly(null, -1, 3, Integer.MAX_VALUE);
}
@Test
public void sortWithNullsFirst() {
List<Integer> list = Lists.newArrayList(Integer.MAX_VALUE, -1, 3, null);
System.out.println(list);
Collections.sort(list, Comparator.nullsFirst(Comparator.comparing(s -> s)));
System.out.println(list);
assertThat(list).containsExactly(null, -1, 3, Integer.MAX_VALUE);
}
@Test
public void sortWithNullsLast() {
List<Integer> list = Lists.newArrayList(Integer.MAX_VALUE, -1, 3, null);
System.out.println(list);
Collections.sort(list, Comparator.nullsLast(Comparator.comparing(s -> s)));
System.out.println(list);
assertThat(list).containsExactly(-1, 3, Integer.MAX_VALUE, null);
}
}
心得分享
Java Collections Sort Example 提供了幾種 Sort 常見方法的操作範例,對不同類型的資料進行排序,像是字串、數字和物件,對資料進行排序可能很有用,例如:產品可以根據用戶設定的要求,像是:價格、評級、品牌,以多種方式排序,熟悉 Collections Sort Java 這些方法的操作,讓排序依照不同的需求,快速方便地實現功能。