java8中Stream流引入函数式编程思想,主要配合各种接口、lambda表达式、方法引用等方式,为集合的遍历、过滤、映射等提供非常“优雅”的操作方式。
Student.java
public class Student {
private String name;
private int age;
private List<Double> grade;//四门课程成绩
private double all;//综合分数
}
初始化List
List<Student> studentList=new ArrayList<>();
studentList.add(new Student("Alex", 18, Arrays.asList(99.6, 82.5, 76.0, 100.0),6.1));
studentList.add(new Student("Claire", 37, Arrays.asList(78.6, 45.5, 77.0, 63.0),5.5));
studentList.add(new Student("Phil", 40, Arrays.asList(63.3, 85.1, 76.4, 96.7),5.6));
studentList.add(new Student("Mitchell",33, Arrays.asList(98.6, 82.5, 76.0, 70.0),8.0));
studentList.add(new Student("Manny", 18, Arrays.asList(97.6, 52.5, 76.0, 88.0),7.0));
过滤操作
List<Student> studentList=getList();
List<Student> s1=studentList.stream().filter(e->e.getAge()>18).collect(Collectors.toList());//过滤
s1.forEach(e->System.out.println(e.getName()));
List<String> nameList=studentList.stream().map(e->e.getName()).collect(Collectors.toList());//将列表中得某一列取出
nameList.forEach(System.out::println);
List<Integer> age=studentList.stream().map(e->e.getAge()).distinct().collect(Collectors.toList());//所有人年龄(去重)
age.forEach(System.out::println);
int num=(int)studentList.stream().filter(e->e.getGrade().get(1)<60).count();//统计第二门课程不及格得人数(count返回Long类型)
System.out.println(num);
//运行结果:
Claire
Phil
Mitchell
//
Alex
Claire
Phil
Mitchell
Manny
18
37
40
33
2
最大值、最小值、平均值
/************求最大最小值*************/
Collections.max(studentList,Comparator.comparingInt(Student::getAge)).getAge();
IntSummaryStatistics agestatc=studentList.stream().collect(Collectors.summarizingInt(Student::getAge));
System.out.println(agestatc.getMax());
System.out.println(agestatc.getMin());
System.out.println(agestatc.getSum());
System.out.println(agestatc.getAverage()); //平均年龄
System.out.println(agestatc.getCount());
//运行结果
40
18
146
29.2
5
/****************排序******************/
List<Student> s1=studentList.stream().sorted((e1,e2)->(int)Math.ceil(e1.getGrade().get(0)-e2.getGrade().get(0))).collect(Collectors.toList());//按照第一门成绩升序排列
List<Student> s2= studentList.stream().sorted(Comparator.comparingDouble(Student::getAll)).collect(Collectors.toList());
List<Student> s3=studentList.stream().sorted(Comparator.comparingDouble(Student::getAll).reversed()).collect(Collectors.toList());//降序
s1.forEach(item->System.out.print(item.getName()+" "));
System.out.println();
s2.forEach(item->System.out.print(item.getName()+" "));
System.out.println();
s3.forEach(item->System.out.print(item.getName()+" "));
System.out.println();
/******************分组 key,value*********************/
Map<Double,List<Student>> map=studentList.stream().collect(Collectors.groupingBy(e->e.getGrade().get(2)));//按照第三门成绩key分组
map.forEach((k,v)->System.out.println("第三门成绩:"+k+"组大小:"+v.size()));
/*******************对集合中每个元素进行操作*******************************/
List<Student> s4=studentList.stream().map(e->{e.setAll(e.getAll()+10); return e;}).collect(Collectors.toList());
s4.forEach(e->System.out.println(e.getAll()));
//运行结果
Phil Claire Manny Mitchell Alex
Claire Phil Alex Manny Mitchell
Mitchell Manny Alex Phil Claire
第三门成绩:77.0组大小:1
第三门成绩:76.0组大小:3
第三门成绩:76.4组大小:1
16.1
15.5
15.6
18.0
17.0
总结: 之前学习了一点,无奈过了段时间还是会忘记怎么使用。。。索性就写成实例,下次再忘记可以迅速回忆起来。后续用到其他了再补充更新~