我开始使用java8的时候,也是新到一家公司,当时使用java版本比较新,就引用了java8新的特性, lambda表达式处理集合遍历,收集,分组,查询等操作,开始觉得脱离了原有的for循环繁琐的遍历,也是挺感兴趣的,于是买了一本java8 lambda 处理流的书籍,其实最后也是没有认真完全看下来,其中说了很多底层的数据结构。然而实际开发中关于 lambda 表达式其实只要会几种操作就完全可以应付日常的开发工作。下面我会介绍针对我们日常开发的小伙伴说lambda表达式的开发中经常使用的操作。
// Java 8之前: new Thread(new Runnable() { @Override public void run() { System.out.println("hello world !"); } }).start(); // Java 8的使用方式: new Thread( () -> System.out.println("hello,world!") ).start(); 大家相比较是不是觉得代码整洁简单许多。接下来我也不介绍java8的新特性的原理和比较,我只针对在实际开发中我们会用 java8怎么处理我业务逻辑代码。
首先我们创建一个学生成绩类
/*** * 学生成绩表 * @author lovewell * */ @Data @ApiModel public class StudentGrade implements Serializable{ /** * */ private static final long serialVersionUID = 5656670486884778407L; @ApiModelProperty(value = "id") private Integer id; @ApiModelProperty(value = "学生姓名") private String name; @ApiModelProperty(value = "课程科目") private String subject; @ApiModelProperty(value = "成绩") private double grade; }
我们书写个测试类给对象添加数据,然后我们使用java8 表达式类处理常见的操作
public class StudentTest { public static void main(String[] args) { List<StudentGrade> studentGrades = new ArrayList<StudentGrade>();
studentGrades.add(new StudentGrade(1, "張三", "语文", 90));
studentGrades.add(new StudentGrade(2, "張三", "数学", 95));
studentGrades.add(new StudentGrade(3, "張三", "英语", 100));
studentGrades.add(new StudentGrade(4, "李四", "语文", 85));
studentGrades.add(new StudentGrade(5, "李四", "数学", 80));
studentGrades.add(new StudentGrade(6, "李四", "英语", 90));
System.out.println(studentGrades);
编译后 :
[StudentGrade(id=1, name=張三, subject=语文, grade=90), StudentGrade(id=2, name=張三, subject=数学, grade=95), StudentGrade(id=3, name=張三, subject=英语, grade=100),
StudentGrade(id=4, name=李四, subject=语文, grade=85), StudentGrade(id=5, name=李四, subject=数学, grade=80), StudentGrade(id=6, name=李四, subject=英语, grade=90)]
}
//比如,我们想找 id=1 或者张三学生语文成绩 的数据 ,可以这样写 StudentGrade grade = studentGrades.stream().filter(studentGrade -> Objects.equals(1, studentGrade.getId())).collect(Collectors.toList()).get(0); System.out.println(grade); StudentGrade grade1 = studentGrades.stream().filter(studentGrade -> Objects.equals("張三", studentGrade.getName())&&Objects.equals("语文", studentGrade.getSubject())) .collect(Collectors.toList()).get(0); System.out.println(grade1);
编译后 :
StudentGrade(id=1, name=張三, subject=语文, grade=90)
StudentGrade(id=1, name=張三, subject=语文, grade=90)
//比如我们想对每个学生分组 就可以这样处理 Map<String, List<StudentGrade>> map = new HashMap<String, List<StudentGrade>>(); map = studentGrades.stream().collect(Collectors.groupingBy(StudentGrade::getName)); System.out.println(map.size()); System.out.println(map.get("張三")); System.out.println(map.get("李四"));
编译后 :
2
[StudentGrade(id=1, name=張三, subject=语文, grade=90), StudentGrade(id=2, name=張三, subject=数学, grade=95), StudentGrade(id=3, name=張三, subject=英语, grade=100)]
[StudentGrade(id=4, name=李四, subject=语文, grade=85), StudentGrade(id=5, name=李四, subject=数学, grade=80), StudentGrade(id=6, name=李四, subject=英语, grade=90)]
//比如我们想遍历集合 ,且想把所有人的成绩改成100分 ,可以这样操作 studentGrades.forEach(studentGrade->{ studentGrade.setGrade(new BigDecimal(100)); }); System.out.println(studentGrades); 编译后 :[StudentGrade(id=1, name=張三, subject=语文, grade=100), StudentGrade(id=2, name=張三, subject=数学, grade=100), StudentGrade(id=3, name=張三, subject=英语, grade=100), StudentGrade(id=4, name=李四, subject=语文, grade=100), StudentGrade(id=5, name=李四, subject=数学, grade=100), StudentGrade(id=6, name=李四, subject=英语, grade=100)]
//比如,我们想计算李四同学的所有科目的总成绩 double totalGrade = studentGrades.stream() .filter(studentGrade->Objects.equals("李四", studentGrade.getName())).mapToDouble(StudentGrade::getGrade).sum(); System.out.println(totalGrade); 编译后 :255.0
//例如我想收集所有同学所有科目中最大分数和最小分数 double max = studentGrades.stream().collect(Collectors.maxBy(Comparator.comparingDouble(StudentGrade::getGrade))).map(StudentGrade::getGrade).get(); double min = studentGrades.stream().collect(Collectors.minBy(Comparator.comparingDouble(StudentGrade::getGrade))).map(StudentGrade::getGrade).get();
// 求平均值 double avg = studentGrades.stream().collect(Collectors.averagingDouble(StudentGrade::getGrade)); // 对集合一级分组 Map<String, List<StudentGrade>> collect = studentGrades.stream().collect(Collectors.groupingBy(StudentGrade-> { if (StudentGrade.getGrade()<60) { return "不及格"; } else if (StudentGrade.getGrade() >= 60 && StudentGrade.getGrade()<70) { return "及格"; }else if (StudentGrade.getGrade()>= 70 && StudentGrade.getGrade()<90) { return "中等"; }else { return "优秀"; } })); //对集合二级分组 Map<String, Map<String, List<StudentGrade>>> collect1 = studentGrades.stream().collect(Collectors.groupingBy(StudentGrade -> { if (StudentGrade.getGrade()<60) { return "不及格"; } else if (StudentGrade.getGrade() >= 60 && StudentGrade.getGrade()<70) { return "及格"; }else if (StudentGrade.getGrade()>= 70 && StudentGrade.getGrade()<90) { return "中等"; }else { return "优秀"; } }, Collectors.groupingBy(StudentGrade::getSubject))); // 对集合分组进行统计数量 final Map<String, Long> collect2 = studentGrades.stream().collect(Collectors.groupingBy(StudentGrade -> { if (StudentGrade.getGrade()<60) { return "不及格"; } else if (StudentGrade.getGrade() >= 60 && StudentGrade.getGrade()<70) { return "及格"; }else if (StudentGrade.getGrade()>= 70 && StudentGrade.getGrade()<90) { return "中等"; }else { return "优秀"; } }, Collectors.counting())); // 将对象的属性转化成相应的key value形式 Map<Object, Double> ljMap2 = studentGrades.stream() .collect(Collectors.toMap(lj -> lj.getName(), StudentGrade::getGrade)); //将对象集合中的属性数据转换到新的集合(集合间数据转换) Set<String> names = studentGrades.stream().map(StudentGrade::getName).collect(Collectors.toSet()); // 通过条件过滤数据 List<StudentGrade> List = studentGrades.stream() .filter(emp -> emp.getGrade() >= 80 && emp.getGrade() <= 100) .collect(Collectors.toList()); // 单条件排序 List<StudentGrade> sortList = studentGrades.stream().sorted(Comparator.comparing(StudentGrade::getGrade)) .collect(Collectors.toList()); // 多条件排序 List<StudentGrade> sortListTwo = studentGrades.stream().sorted(Comparator.comparing(StudentGrade::getGrade) .thenComparing(StudentGrade::getName).reversed()) .collect(Collectors.toList()); // 统计计数 Long count=studentGrades.stream().collect(Collectors.counting()); // 取最大或小值(大maxBy 小 minBy) StudentGrade studentGrade = studentGrades.stream().collect(Collectors.maxBy(Comparator.comparingDouble(StudentGrade::getGrade))).get(); // 成绩求和 double sum = studentGrades.stream().collect(Collectors.summingDouble(StudentGrade::getGrade));
End 谢谢各位小伙伴阅读,略有帮助的话加个关注,私下有问题可以讨教。