引言
在前面,写过java8实战系列文章,文章的内容都是基于《Java8 in Action》一书学习总结的。
这段时间在项目中很多地方都用到了,但很多时候都需要现查一下该怎么写,本篇博客就来总结一下关于List的一些常用的流操作。
基础代码
学生实体代码如下:
@Data
public 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转换为指定字符分隔的字符串
@Test
public 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
@Test
public void stuMap() {
List<Student> students = buildStudentList();
Map<Integer, Student> studentMap = students.stream().collect(Collectors.toMap(Student::getId, s -> s, (s1, s2) -> s1));
System.out.println(studentMap);
}
3. Collectors.groupingBy:分组
@Test
public void stuGroup() {
List<Student> students = buildStudentList();
Map<String, List<Student>> classMap = students.stream().collect(Collectors.groupingBy(Student::getClassName));
System.out.println(classMap);
}
4. filter:过滤
@Test
public void stuFilter() {
List<Student> students = buildStudentList();
List<Student> list = students.stream().filter(s -> s.getProvince().equals("北京")).collect(Collectors.toList());
System.out.println(list);
}
5. Collectors.summarizingDouble:统计
@Test
public void stuStatistics() {
List<Student> students = buildStudentList();
Map<String, DoubleSummaryStatistics> classMap = students.stream().collect(Collectors.groupingBy(Student::getClassName, Collectors.summarizingDouble(Student::getScore)));
System.out.println(classMap);
System.out.println(classMap.get("A").getSum());
System.out.println(classMap.get("B").getMax());
System.out.println(classMap.get("A").getMin());
System.out.println(classMap.get("A").getAverage());
System.out.println(classMap.get("C").getCount());
}
6. Collectors.toCollection:set去重
@Test
public void stuSet() {
List<Student> students = buildStudentList();
ArrayList<Student> list = students.stream().collect(Collectors.collectingAndThen(Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(Student::getProvince))), ArrayList::new));
System.out.println(list);
}
7. max和min:最大值和最小值
@Test
public void stuMaxMin() {
List<Student> students = buildStudentList();
students.stream().max(Comparator.comparing(Student::getAge)).ifPresent(System.out::println);
students.stream().min(Comparator.comparing(Student::getAge)).ifPresent(System.out::println);
}
8. sorted:排序
@Test
public void stuSort() {
List<Student> students = buildStudentList();
List<Student> list = students.stream().filter(s -> "B".equals(s.getClassName())).sorted(Comparator.comparing(Student::getAge)).collect(Collectors.toList());
System.out.println(list);
List<Student> listReverse = students.stream().filter(s -> "B".equals(s.getClassName())).sorted(Comparator.comparing(Student::getScore).reversed()).collect(Collectors.toList());
System.out.println(listReverse);
}
总结
下面是流的常用方法总结,可根据实际场景选择使用:

在实际项目中,真正体会到有时候复杂的需求,直接用流操作,能很容易实现,并且代码上简化了很多。
PS:又是一年1024,程序员(媛)们节日快乐!