JDK 1.8 Lambda表达式集合分组、条件过滤、组装、去重、排序、转换、求和、最值

一、JDK 1.8 Lambda简述

在jdk1.8中lambda表达式提供了集合的一些很实用的操作方法,函数式编程既减少了很多繁杂的foreach代码,又简洁明了,通俗易懂.

二、Lambda表达式

Lambda表达式本质上是一段匿名内部类,也可以是一段可以传递的代码

三、上干货

1. 新建一个Bean( User),新建一个List list = new Array<>(); 并把多个student添加到list中

public class Student {
        private String no;
        private String name;
        private String classes;

        public Student(String no, String name, String classes) {
            this.no = no;
            this.name = name;
            this.classes = classes
        }

        public String getNo() {
            return no;
        }

        public void setNo(String no) {
            this.no = no;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public String getClasses() {
            return classes;
        }

        public void setClasses(String classes) {
            this.classes = classes;
        }
    }

2. 新建一个List list = new Array<>(); 并把多个student添加到list中

List<Student> list = new ArrayList<>();
list.add(new Student("4","小米","二班"));
list.add(new Student("2","小丽","一班"));
list.add(new Student("1","小红","一班"));
list.add(new Student("3","小名","二班"));
list.add(new Student("3","小名","二班"));

3. 按照班级分组groupingBy()

 Map<String, List<Student>> listMap = list.stream().collect(Collectors.groupingBy(Student::getClasses));

4. 筛选二班的学生filter()

List<Student> filterList = list.stream().filter(student -> student.getClass().equals("二班")).collect(Collectors.toList());

5. 获取list某个字段组装新list map()

List<String> newList = list.stream().map(student -> student.getName()).collect(Collectors.toList());

6. 去重distinct().

List<Student> filterList = list.stream().distinct().collect(Collectors.toList());
//去重
List<Map<String, String>> list = teacherList.stream().collect(Collectors.toMap(m -> m.get("course") + m.get("teacherTel"), Function.identity(), (oldValue, newValue) -> oldValue)).values().stream().collect(Collectors.toList());

7. 单字段多字段排序sort()、sorted()

//第一种
list.sort(Comparator.comparing(Student::getNo));
//注:若选择排序字段为null值,正序可
list.sort(Comparator.comparing(Student::getNo,Comparator.nullsFirst(Comparator.naturalOrder())));
//第二种
List<Student> sorterList = list.stream().sorted(Comparator.comparing(Student::getNo)).collect(Collectors.toList());
//第三种
List<Student> sorterList = list.stream().sorted((s1,s2)->{return s1.getNo().compareTo(s2.getNo());}).collect(Collectors.toList());

8. 多字段排序sort()、sorted()

//第一种
list.sort(Comparator.comparing(Student::getNo).thenComparing(Comparator.comparing(Student::getName)));
//第二种
List<Student> sorterList = list.stream().sorted(Comparator.comparing(Student::getNo).thenComparing(Student::getName)).collect(Collectors.toList());
//第三种
List<Student> sorterList = list.stream().sorted((s1, s2) -> {
         int v1 = s1.getNo().compareTo(s2.getNo());
         if (v1 != 0)
             return v1;
         int v2 = s1.getName().compareTo(s2.getName());
         return v2;
     }).collect(Collectors.toList());

9. list转map

//其中,若集合对象key有重,可根据(k1,k2)->k1设置<保留k1,舍弃k2>  
Map<String, Student> studentMap = list.stream().collect(Collectors.toMap(Student::getNo, t -> t, (k1, k2) -> k1));

10. 求和

int sum = list.stream().mapToInt(s->Integer.valueOf(s.getNo())).sum();

11. 最大值

int max = list.stream().mapToInt(s -> Integer.valueOf(s.getNo())).max().getAsInt();

评论

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×