@Data
public static class Student{
private String id;
private String name;
private Double score;
private Integer age;
private Integer sex;
}
public static void main(String[] args) {
//过滤
List<Student> listFilter = list.stream().filter(item -> item.getAge() > 14)
.collect(Collectors.toList());
System.out.println("过滤:"+JSONArray.toJSONString(listFilter));
//排序
List<Student> sortedList = list.stream().sorted(Comparator.comparing(Student::getScore).reversed()).collect(Collectors.toList());
//正序List<Student> sortedList = list.stream().sorted(Comparator.comparing(Student::getScore)).collect(Collectors.toList());
System.out.println("分数排行:"+JSONArray.toJSONString(sortedList));
//过滤并排序
List<Student> listFilterSort = list.stream().filter(item -> item.getAge() > 14)
.sorted(Comparator.comparing(Student::getScore))
.collect(Collectors.toList());
System.out.println("过滤排序:"+JSONArray.toJSONString(listFilterSort));
//求总分
double sum = list.stream().mapToDouble(Student::getScore).sum();
System.out.println("总分:"+JSONArray.toJSONString(sum));
//获取list某个字段组装新list
List<String> nameList = list.stream().map(item -> item.getName()).collect(Collectors.toList());
System.out.println("学生名单:"+ JSONArray.toJSONString(nameList));
//去重
List<Student> disList = list.stream().collect(
Collectors.collectingAndThen(
Collectors.toCollection(() -> new TreeSet<>(Comparator
.comparing(Student::getName))), ArrayList::new));
System.out.println("去重:"+JSONArray.toJSONString(disList));
//分组
Map<Integer, List<Student>> groupList = list.stream().collect(Collectors.groupingBy(Student::getSex));
System.out.println("分组:"+JSONArray.toJSONString(groupList));
//根据两个属性去重
List<Student> disTwo = list.stream().collect(
Collectors.collectingAndThen(
Collectors.toCollection(() -> new TreeSet<>(
Comparator.comparing(o -> o.getId() + ";" + o.getSex()))), ArrayList::new));
System.out.println("根据两个属性去重:"+JSONArray.toJSONString(disTwo));
//基本类型列表去重
List<Long> li = versionIds.stream().distinct().collect(Collectors.toList());
}
过滤:[{"age":15,"id":"1","name":"李四","score":95.5,"sex":0},{"age":16,"id":"2","name":"张三","score":90,"sex":1},{"$ref":"$[0]"},{"$ref":"$[1]"}]
分数排行:[{"age":15,"id":"1","name":"李四","score":95.5,"sex":0},{"age":16,"id":"2","name":"张三","score":90,"sex":1},{"age":14,"id":"2","name":"王二","score":85.5,"sex":1},{"age":14,"id":"2","name":"王二","score":85.5,"sex":1}]
过滤排序:[{"age":16,"id":"2","name":"张三","score":90,"sex":1},{"age":15,"id":"1","name":"李四","score":95.5,"sex":0}]
总分:356.5
学生名单:["李四","张三","王二","王二"]
去重:[{"age":16,"id":"2","name":"张三","score":90,"sex":1},{"age":15,"id":"1","name":"李四","score":95.5,"sex":0},{"age":14,"id":"2","name":"王二","score":85.5,"sex":1}]
分组:{0:[{"age":15,"id":"1","name":"李四","score":95.5,"sex":0}],1:[{"age":16,"id":"2","name":"张三","score":90,"sex":1},{"age":14,"id":"2","name":"王二","score":85.5,"sex":1},{"age":14,"id":"2","name":"王二","score":85.5,"sex":1}]}
根据两个属性去重:[{"age":15,"id":"1","name":"李四","score":95.5,"sex":0},{"age":16,"id":"2","name":"张三","score":90,"sex":1}]
网友评论