package lambda.list;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import pojo.Dome;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;
/**
* @Author: xxx
* @date 2021/5/14
**/
@Slf4j
public class SortTest {
/***
* 初始化测试数组
* @date 2021/5/14
**/
private List<Dome> initArray() {
Dome dome1 = new Dome("张三", 11.0, 10);
Dome dome2 = new Dome("李四", 61.0, 11);
Dome dome3 = new Dome("王五", 26.0, 12);
Dome dome4 = new Dome("张三", 14.0, 14);
Dome dome5 = new Dome("王五", 24.0, 15);
Dome dome6 = new Dome("李四", 16.0, 16);
Dome dome7 = new Dome("赵六", 35.0, 17);
List<Dome> domes = new ArrayList<>();
domes.add(dome1);
domes.add(dome2);
domes.add(dome3);
domes.add(dome4);
domes.add(dome5);
domes.add(dome6);
domes.add(dome7);
log.info("domes : {}", domes);
return domes;
}
/***
* List 排序正序
* @date 2021/5/13
**/
@Test
public void sortTest1() {
List<Dome> domes = initArray();
List<Dome> collect = domes.stream().sorted((s1, s2) -> s1.getMoney().compareTo(s2.getMoney())).collect(Collectors.toList());
log.info("collect : {}", collect);
domes.sort((s1, s2) -> s2.getMoney().compareTo(s1.getMoney()));
log.info("domes : {}", domes);
/*
domes : [Dome{name='张三', money=11.0, age=10}, Dome{name='李四', money=61.0, age=11}, Dome{name='王五', money=26.0, age=12}, Dome{name='张三', money=14.0, age=14}, Dome{name='王五', money=24.0, age=15}, Dome{name='李四', money=16.0, age=16}, Dome{name='赵六', money=35.0, age=17}]
collect : [Dome{name='张三', money=11.0, age=10}, Dome{name='张三', money=14.0, age=14}, Dome{name='李四', money=16.0, age=16}, Dome{name='王五', money=24.0, age=15}, Dome{name='王五', money=26.0, age=12}, Dome{name='赵六', money=35.0, age=17}, Dome{name='李四', money=61.0, age=11}]
domes : [Dome{name='李四', money=61.0, age=11}, Dome{name='赵六', money=35.0, age=17}, Dome{name='王五', money=26.0, age=12}, Dome{name='王五', money=24.0, age=15}, Dome{name='李四', money=16.0, age=16}, Dome{name='张三', money=14.0, age=14}, Dome{name='张三', money=11.0, age=10}]
*/
}
/***
* List 排序倒序
* @date 2021/5/13
**/
@Test
public void sortTest2() {
List<Dome> domes = initArray();
List<Dome> collect = domes.stream().sorted(Comparator.comparing(Dome::getMoney).reversed()).collect(Collectors.toList());
log.info("collect : {}", collect);
/*
domes : [Dome{name='张三', money=11.0, age=10}, Dome{name='李四', money=61.0, age=11}, Dome{name='王五', money=26.0, age=12}, Dome{name='张三', money=14.0, age=14}, Dome{name='王五', money=24.0, age=15}, Dome{name='李四', money=16.0, age=16}, Dome{name='赵六', money=35.0, age=17}]
collect : [Dome{name='李四', money=61.0, age=11}, Dome{name='赵六', money=35.0, age=17}, Dome{name='王五', money=26.0, age=12}, Dome{name='王五', money=24.0, age=15}, Dome{name='李四', money=16.0, age=16}, Dome{name='张三', money=14.0, age=14}, Dome{name='张三', money=11.0, age=10}]
*/
}
/**
* 对二维数组{x,y}的y进行从小到大排序
*
* 方法1:
* 重写比较器写法
*/
@Test
public void sortTest3() {
int[][] nums = {{3, 1}, {4, 2}};
Arrays.sort(nums,new Comparator<int[]>(){
@Override
public int compare(int[] a,int[] b){
if(a[1]==b[1]){
return a[0]-b[0];
}else{
return a[1]-b[1];
}
}
});
for (int i = 0; i < nums.length; i++) {
for (int j = 0; j < nums[0].length; j++) {
System.out.print(nums[i][j]+" ");
}
System.out.println();
}
}
/**
* 对二维数组{x,y}的y进行从小到大排序
*
* 方法2:
* Lambda表达式写法
*/
@Test
public void sortTest4(){
int[][] nums = {{3, 1}, {4, 2}};
/*Arrays.sort(nums, (a, b) -> {
return a[1] == b[1] ? a[0] - b[0] : a[1] - b[1];
});*/
Arrays.sort(nums, (a, b) -> a[1] == b[1] ? a[0] - b[0] : a[1] - b[1]);
for (int i = 0; i < nums.length; i++) {
for (int j = 0; j < nums[0].length; j++) {
System.out.print(nums[i][j]+" ");
}
System.out.println();
}
}
/**
* 对二维数组{x,y}的y进行从小到大排序
*
* 方法3:
* 更简洁的Lambda表达式
*/
@Test
public void sortTest5(){
int[][] nums = {{3, 1}, {4, 0}, {8, 5}, {9, -8}};
/* Arrays.sort(nums, (o1, o2) -> o1[1] - o2[1]); */
Arrays.sort(nums, Comparator.comparingInt(o -> o[1]));
for (int i = 0; i < nums.length; i++) {
for (int j = 0; j < nums[0].length; j++) {
System.out.print(nums[i][j]+" ");
}
System.out.println();
}
}
/**
* 多字段排序 优化
*/
@Test
public void sortTest6(){
List<Dome> domes = initArray();
//单字段排序,根据id排序
domes.sort(Comparator.comparing(Dome::getAge));
//多字段排序,根据id,年龄排序
domes.sort(Comparator.comparing(Dome::getAge).thenComparing(Dome::getMoney));
//普通的排序取值
List<Dome> list11 = domes.stream().sorted((u1, u2) -> u1.getMoney().compareTo(u2.getMoney())).limit(3).collect(Collectors.toList());
System.out.println("排序之后的数据:" + list11);
//优化排序取值
List<Dome> list12 = domes.stream().limit(3).sorted((u1, u2) -> u1.getMoney().compareTo(u2.getMoney())).collect(Collectors.toList());
System.out.println("优化排序之后的数据:" + list12);
//排序之后的数据:[{"id":1,"name":"pancm1"}, {"id":2,"name":"pancm2"}, {"id":3,"name":"pancm3"}]
//优化排序之后的数据:[{"id":1,"name":"pancm1"}, {"id":2,"name":"pancm2"}, {"id":3,"name":"pancm3"}]
}
}