引言
在前面,写过java8实战系列文章,文章的内容都是基于《Java8 in Action》一书学习总结的。
这段时间在项目中很多地方都用到了,但很多时候都需要现查一下该怎么写,本篇博客就来总结一下关于List的一些常用的流操作。
基础代码
学生实体代码如下:
@Datapublic class Student implements Serializable {/*** 编号*/private Integer id;/*** 姓名*/private String name;/*** 年龄*/private Integer age;/*** 班级*/private String className;/*** 所在省*/private String province;/*** 学号*/private Integer stuNo;/*** 入学成绩*/private Double score;public Student(Integer id, String name, Integer age, String className, String province, Integer stuNo, Double score) {this.id = id;this.name = name;this.age = age;this.className = className;this.province = province;this.stuNo = stuNo;this.score = score;}}
学生list代码如下:
private List<Student> buildStudentList() {return Arrays.asList(new Student(1, "张扬", 18, "A", "北京", 45, 83.2),new Student(2, "李丹", 22, "A", "天津", 15, 65.5),new Student(3, "张丹", 22, "B", "山东", 44, 78.4),new Student(4, "白天", 19, "B", "北京", 1, 63.7),new Student(5, "王武", 20, "C", "湖南", 34, 78.3));}
下面将通过对学生list的操作来展示如何使用java8中的stream来对list进行转换、分组、过滤、求和、排序等操作。
流的使用
1. Collectors.joining:list转换为指定字符分隔的字符串
/*** list转字符串,指定逗号字符分隔*/@Testpublic void stuJoin() {List<Student> students = buildStudentList();//集合中的名字用逗号分隔String names = students.stream().map(Student::getName).collect(Collectors.joining(","));//结果:张扬,李丹,张丹,白天,王武System.out.println(names);}
2. Collectors.toMap:list转换为map
/*** list转map* toMap 如果集合对象有重复的key,会报错Duplicate key ....* word1,word2的id都为1。* 可以用 (k1,k2)->k1 来设置,如果有重复的key,则保留key1,舍弃key2*/@Testpublic void stuMap() {List<Student> students = buildStudentList();Map<Integer, Student> studentMap = students.stream().collect(Collectors.toMap(Student::getId, s -> s, (s1, s2) -> s1));//结果:{1=Student(id=1, name=张扬, age=18, className=A, province=北京, stuNo=45, score=83.2), 2=Student(id=2, name=李丹, age=22, className=A, province=天津, stuNo=15, score=65.5), 3=Student(id=3, name=张丹, age=22, className=B, province=山东, stuNo=44, score=78.4), 4=Student(id=4, name=白天, age=19, className=B, province=北京, stuNo=1, score=63.7), 5=Student(id=5, name=王武, age=20, className=C, province=湖南, stuNo=34, score=78.3)}System.out.println(studentMap);}
3. Collectors.groupingBy:分组
/*** 按对象指定属性分组*/@Testpublic void stuGroup() {List<Student> students = buildStudentList();//按照班级进行分组Map<String, List<Student>> classMap = students.stream().collect(Collectors.groupingBy(Student::getClassName));//结果:{A=[Student(id=1, name=张扬, age=18, className=A, province=北京, stuNo=45, score=83.2), Student(id=2, name=李丹, age=22, className=A, province=天津, stuNo=15, score=65.5)], B=[Student(id=3, name=张丹, age=22, className=B, province=山东, stuNo=44, score=78.4), Student(id=4, name=白天, age=19, className=B, province=北京, stuNo=1, score=63.7)], C=[Student(id=5, name=王武, age=20, className=C, province=湖南, stuNo=34, score=78.3)]}System.out.println(classMap);}
4. filter:过滤
/*** 过滤出某个值的集合*/@Testpublic void stuFilter() {List<Student> students = buildStudentList();//筛选出省份为北京的学生List<Student> list = students.stream().filter(s -> s.getProvince().equals("北京")).collect(Collectors.toList());//结果:[Student(id=1, name=张扬, age=18, className=A, province=北京, stuNo=45, score=83.2), Student(id=4, name=白天, age=19, className=B, province=北京, stuNo=1, score=63.7)]System.out.println(list);}
5. Collectors.summarizingDouble:统计
/*** 分组后统计数据(集合个数、分数求和、最小值、平均值、最大值)*/@Testpublic void stuStatistics() {List<Student> students = buildStudentList();//按照班级进行分组Map<String, DoubleSummaryStatistics> classMap = students.stream().collect(Collectors.groupingBy(Student::getClassName, Collectors.summarizingDouble(Student::getScore)));//结果:{A=DoubleSummaryStatistics{count=2, sum=148.700000, min=65.500000, average=74.350000, max=83.200000}, B=DoubleSummaryStatistics{count=2, sum=142.100000, min=63.700000, average=71.050000, max=78.400000}, C=DoubleSummaryStatistics{count=1, sum=78.300000, min=78.300000, average=78.300000, max=78.300000}}System.out.println(classMap);//分组后指定集合的总和:148.7System.out.println(classMap.get("A").getSum());//分组后指定集合的最大值:78.4System.out.println(classMap.get("B").getMax());//分组后指定集合的最小值:65.5System.out.println(classMap.get("A").getMin());//分组后指定集合的平均值:74.35System.out.println(classMap.get("A").getAverage());//分组后指定集合的个数:1System.out.println(classMap.get("C").getCount());}
6. Collectors.toCollection:set去重
/*** 去重*/@Testpublic void stuSet() {List<Student> students = buildStudentList();ArrayList<Student> list = students.stream().collect(Collectors.collectingAndThen(Collectors.toCollection(() -> new TreeSet<>(paring(Student::getProvince))), ArrayList::new));//结果:[Student(id=1, name=张扬, age=18, className=A, province=北京, stuNo=45, score=83.2), Student(id=2, name=李丹, age=22, className=A, province=天津, stuNo=15, score=65.5), Student(id=3, name=张丹, age=22, className=B, province=山东, stuNo=44, score=78.4), Student(id=5, name=王武, age=20, className=C, province=湖南, stuNo=34, score=78.3)]System.out.println(list);}
7. max和min:最大值和最小值
/*** 最大值和最小值*/@Testpublic void stuMaxMin() {List<Student> students = buildStudentList();//结果:Student(id=2, name=李丹, age=22, className=A, province=天津, stuNo=15, score=65.5)students.stream().max(paring(Student::getAge)).ifPresent(System.out::println);//结果:Student(id=1, name=张扬, age=18, className=A, province=北京, stuNo=45, score=83.2)students.stream().min(paring(Student::getAge)).ifPresent(System.out::println);}
8. sorted:排序
/*** 排序*/@Testpublic void stuSort() {List<Student> students = buildStudentList();//筛选B班的学生,并按年龄从小到大排序List<Student> list = students.stream().filter(s -> "B".equals(s.getClassName())).sorted(paring(Student::getAge)).collect(Collectors.toList());//结果:[Student(id=4, name=白天, age=19, className=B, province=北京, stuNo=1, score=63.7), Student(id=3, name=张丹, age=22, className=B, province=山东, stuNo=44, score=78.4)]System.out.println(list);//筛选B班的学生,并分数从高到低List<Student> listReverse = students.stream().filter(s -> "B".equals(s.getClassName())).sorted(paring(Student::getScore).reversed()).collect(Collectors.toList());//结果:[Student(id=3, name=张丹, age=22, className=B, province=山东, stuNo=44, score=78.4), Student(id=4, name=白天, age=19, className=B, province=北京, stuNo=1, score=63.7)]System.out.println(listReverse);}