排序工具類
@Data
@AllArgsConstructor
@ToString
public class Student {
private Long id;
private String name;
private Integer score;
}
public class ListSortExample {
private List<Student> studentList = Lists.newArrayList();
@Before
public void init(){
studentList.add(new Student(1001L, "zhuoli", 99));
studentList.add(new Student(999L, "Alice", 87));
studentList.add(new Student(1345L, "Michael", 90));
studentList.add(new Student(1024L, "Jane", 99));
}
@Test
public void testSortWithStream(){
List<Student> sortedList = studentList.stream().sorted(Comparator.comparing(Student::getScore).reversed()).collect(Collectors.toList());
System.out.println(studentList);
System.out.println(sortedList);
List<Student> sortedList1 = studentList.stream().sorted(Comparator.comparing(Student::getScore).thenComparing(Student::getName).reversed()).collect(Collectors.toList());
System.out.println(sortedList1);
}
@Test
public void testSort(){
Comparator<Student> compareByScoreAndNameReverse = Comparator.comparing(Student::getScore).thenComparing(Student::getName);
studentList.sort(compareByScoreAndNameReverse);
System.out.println(studentList);
}
@Test
public void testSort1(){
Comparator<Student> compareByScoreAndNameReverse = Comparator.comparing(Student::getScore).thenComparing(Student::getName);
Collections.sort(studentList, compareByScoreAndNameReverse);
System.out.println(studentList);
}
@Test
public void customSort (){
final List<String> sortList = Arrays.asList("Alice","Michael","Jane","zhuoli");
List<Student> result = studentList.stream().sorted(Comparator.comparing(Student::getName, (x, y) -> {
for(String sort : sortList){
if(sort.equals(x) || sort.equals(y)){
if(x.equals(y)){
return 0;
}else if(sort.equals(x)){
return -1;
}else{
return 1;
}
}
}
return 0;
})).collect(Collectors.toList());
System.out.println(result);
}
}
public class SortByKeyExample {
@Test
public void SortByKeyTest() {
Map<String, Integer> unsortMap = new HashMap<>();
unsortMap.put("z", 10);
unsortMap.put("b", 5);
unsortMap.put("a", 6);
unsortMap.put("c", 20);
unsortMap.put("d", 1);
unsortMap.put("e", 7);
unsortMap.put("y", 8);
unsortMap.put("n", 99);
unsortMap.put("g", 50);
unsortMap.put("m", 2);
unsortMap.put("f", 9);
System.out.println("Original...");
System.out.println(unsortMap);
Map<String, Integer> result = unsortMap.entrySet().stream()
.sorted(Map.Entry.comparingByKey())
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue,
(oldValue, newValue) -> oldValue, LinkedHashMap::new));
Map<String, Integer> result2 = new LinkedHashMap<>();
unsortMap.entrySet().stream()
.sorted(Map.Entry.comparingByKey())
.forEachOrdered(x -> result2.put(x.getKey(), x.getValue()));
System.out.println("Sorted...");
System.out.println(result);
System.out.println(result2);
}
}```
```java
public class SortByValueExample {
@Test
public void sortByCommonValue() {
Map<String, Integer> unsortMap = new HashMap<>();
unsortMap.put("z", 10);
unsortMap.put("b", 5);
unsortMap.put("a", 6);
unsortMap.put("c", 20);
unsortMap.put("d", 1);
unsortMap.put("e", 7);
unsortMap.put("y", 8);
unsortMap.put("n", 99);
unsortMap.put("g", 50);
unsortMap.put("m", 2);
unsortMap.put("f", 9);
System.out.println("Original...");
System.out.println(unsortMap);
Map<String, Integer> result = unsortMap.entrySet().stream()
.sorted(Map.Entry.comparingByValue(Comparator.reverseOrder()))
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue,
(oldValue, newValue) -> oldValue, LinkedHashMap::new));
Map<String, Integer> result2 = new LinkedHashMap<>();
unsortMap.entrySet().stream()
.sorted(Map.Entry.<String, Integer>comparingByValue().reversed())
.forEachOrdered(x -> result2.put(x.getKey(), x.getValue()));
System.out.println("Sorted...");
System.out.println(result);
System.out.println(result2);
}
@Test
public void sortedByObjectValueExample(){
List<Student> studentList = Lists.newArrayList();
studentList.add(new Student(1001L, "zhuoli", 99));
studentList.add(new Student(999L, "Alice", 87));
studentList.add(new Student(1345L, "Michael", 90));
studentList.add(new Student(1024L, "Jane", 99));
Map<Long, Student> studentMap = studentList.stream().collect(Collectors.toMap(Student::getId, ele -> ele));
Map<Long, Student> result = studentMap.entrySet().stream()
.sorted(Map.Entry.comparingByValue(Comparator.comparing(Student::getScore).reversed()))
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue,
(oldValue, newValue) -> oldValue, LinkedHashMap::new));
System.out.println(result);
}
}
public class SetSortExample {
private Set<Student> studentSet = Sets.newHashSet();
@Before
public void init(){
studentSet.add(new Student(1001L, "zhuoli", 99));
studentSet.add(new Student(999L, "Alice", 87));
studentSet.add(new Student(1345L, "Michael", 90));
studentSet.add(new Student(1024L, "Jane", 99));
}
@Test
public void setSortTest(){
List<Student> sortedList = studentSet.stream().sorted(Comparator.comparing(Student::getScore).reversed()).collect(Collectors.toList());
System.out.println(sortedList);
LinkedHashSet<Student> sortedSet1 = Sets.newLinkedHashSet();
studentSet.stream().sorted(Comparator.comparing(Student::getScore).reversed()).forEachOrdered(sortedSet1::add);
System.out.println(sortedSet1);
}
}
代碼已傳github,點擊跳轉