zoukankan      html  css  js  c++  java
  • java8+ Lambda表达式基本用法

    LIST

    public class LambdaTest {
    
        @Getter
        @Setter
        @AllArgsConstructor
        static class Student{
            private Long id;
            private String name;
            private Integer age;
        }
    
        public static void main(String[] args) {
            List<Student> studentList = Arrays.asList(new Student(1L,"张三",15),new Student(2L,"李四",11),
                    new Student(3L,"王五",12),new Student(4L,"赵六",10));
            System.out.println("原数据:"+ JSONArray.toJSON(studentList));
            List<Student> collect1 = studentList.stream().filter(stu -> stu.getName().equals("张三")).collect(Collectors.toList());
            System.out.println("查出姓名是张三的学生:"+JSONArray.toJSON(collect1));
            List<Integer> collect2 = studentList.stream().map(stu -> stu.getAge()).collect(Collectors.toList());
            System.out.println("取出学生的年龄集合:"+collect2);
            List<Integer> collect6 = studentList.stream().map(stu -> stu.getAge()).distinct().collect(Collectors.toList());
            System.out.println("取出学生的年龄集合并去重:"+collect6);
            List<Student> collect3 = studentList.stream().skip(3).collect(Collectors.toList());
            System.out.println("学生集合里去除前三个:"+JSONArray.toJSON(collect3));
            List<Student> collect4 = studentList.stream().limit(3).collect(Collectors.toList());
            System.out.println("学生集合里取前三个:"+JSONArray.toJSON(collect4));
            Map<Integer, List<Student>> collect5 = studentList.stream().collect(Collectors.groupingBy(student -> student.getAge()));
            System.out.println("根据学生年龄分组:"+JSONArray.toJSON(collect5));
            int ageSum = studentList.stream().mapToInt(stu -> stu.getAge()).sum();
            System.out.println("找出学生年龄之和:"+ageSum);
            int ageMax= studentList.stream().mapToInt(stu -> stu.getAge()).max().getAsInt();
            System.out.println("找出学生年龄最大:"+ageMax);
            int ageMin= studentList.stream().mapToInt(stu -> stu.getAge()).min().getAsInt();
            System.out.println("找出学生年龄最小:"+ageMin);
            Double aggAverage = studentList.stream().mapToInt(stu -> stu.getAge()).average().getAsDouble();
            System.out.println("找出学生年龄最小:"+aggAverage);
            studentList.sort((a,b) -> b.getAge().compareTo(a.getAge()));
            System.out.println("根据学生年龄降序:"+JSONArray.toJSON(studentList));
            studentList.sort((a,b) -> a.getAge().compareTo(b.getAge()));
            System.out.println("根据学生年龄升序:"+JSONArray.toJSON(studentList));
        }
    
    }
    

    转逗号分隔

    list.stream().collect(Collectors.joining(","))
    

    排序

    升序

    studentList.sort(Comparator.comparing(Student::getAge));
    

    降序

    studentList.sort(Comparator.comparing(Student::getAge).reversed());
    

    分组

    根据传入的size将现有list分组(分页)

    public static <T> List<List<T>> splitList(List<T> in, int size){
            List<List<T>> out = new ArrayList<>();
            int mode = in.size()%size;
            int page = in.size()/size;
            for(int i=1;i<=page;i ++){
                int start = (i-1)*size;
                List<T> sub = in.subList(start,start+size);
                out.add(sub);
            }
            if(mode>0){
                int start = page*size;
                List<T> sub = in.subList(start,start+mode);
                out.add(sub);
            }
            return out;
        }
    
  • 相关阅读:
    jQuery插件开发——元素拖拽和基于鼠标位置为中心缩放
    基于Quartz.Net的任务管理平台开发(3) —— 任务管理平台
    记录一下Linq实现多列排序的问题
    动态构造Lambda表达式
    MVC全局实现异常处理
    修改自增列起始值,仅当使用了列列表并且 IDENTITY_INSERT 为 ON 时,才能为表'******'中的标识列指定显式值
    统计指定时间段内生日的用户
    SQL开关功能记录
    枚举对象实现 DropDownList 的转换操作二
    枚举对象实现 DropDownList 的转换操作一
  • 原文地址:https://www.cnblogs.com/pigmen/p/14088275.html
Copyright © 2011-2022 走看看