1. 如何创建Stream
//new一个集合
List list = new ArrayList<>();
//普通流
Stream stream = list.stream();
//并行流 需要注意线程安全问题
Stream stringStream = list.parallelStream();
2. Stream常用的中间操作
2.1 Map 元素类型转换
//生成一个包含学生的集合
List list = new ArrayList<>();
//获取学生的姓名集合
List nameList = list.stream().map(Student::getName).collect(Collectors.toList());
//获取学生的Id集合
List idList = list.stream().map(Student::getId).collect(Collectors.toList());
2.2 Filter 过滤
//获取姓名是ww的集合
List collect = list.stream().filter(student -> "ww".equals(student.getName())).collect(Collectors.toList());
2.3 Sort 排序
//按照学生的年龄升序排列
List sortedUpStudent = list.stream().sorted(Comparator.comparingInt(Student::getAge)).collect(Collectors.toList());
//按照学生的年龄降序排列
List sortedDowmStudent = list.stream().sorted(Comparator.comparingInt(Student::getAge).reversed()).collect(Collectors.toList());
3. 常用操作
3.1 给list去重
//根据学生的name属性去重
List hostList=list.stream().collect(Collectors
.collectingAndThen(Collectors.toCollection(() ->
new TreeSet<>(Comparator.comparing(Student :: getName))),
ArrayList::new));
3.2 Collectors相关实例
// 累积名字属性到一个集合中
List list = people.stream().map(Person::getName).collect(Collectors.toList());
// 累积名字属性到一个TreeSet中
Set set = people.stream().map(Person::getName).collect(Collectors.toCollection(TreeSet::new));
// 将元素转为String,并串联他们,中间的分隔符是逗号
String joined = things.stream()
.map(Object::toString)
.collect(Collectors.joining(", "));
// 见员工的工资求和
int total = employees.stream()
.collect(Collectors.summingInt(Employee::getSalary)));
// 将员工按部门分组
Map> byDept
= employees.stream()
.collect(Collectors.groupingBy(Employee::getDepartment));
// 求出各部门的薪资总和
Map totalByDept
= employees.stream()
.collect(Collectors.groupingBy(Employee::getDepartment,
Collectors.summingInt(Employee::getSalary)));
// 按照分数及格与否对学生分区
Map> passingFailing =
students.stream()
.collect(Collectors.partitioningBy(s -> s.getGrade() >= PASS_THRESHOLD));
4. 练习实例
// 测试数据
List list = new ArrayList<>();
list.add(new Student("李明", "男", 10D));
list.add(new Student("李飞", "男", 20d));
list.add(new Student("张丽", "女", 30d));
list.add(new Student("张艳", "女", 40d));
list.add(new Student("王朋", "男", 50d));
list.add(new Student("陈晴", "女", 60d));
list.add(new Student("王菲", "女", 70d));
list.add(new Student("李雷", "男", 80d));
list.add(new Student("张三", "中性", 80d));
list.add(new Student("李四", "中性", 80d));
要求返回格式为: (根据性别分组,且金额累加)
[{value: 1048, name: '中性'},{value: 735, name: '男'},{value: 735, name: '女'}]
2.2 Filter 过滤//生成一个包含学生的集合 Listlist = new ArrayList<>(); //获取学生的姓名集合 List nameList = list.stream().map(Student::getName).collect(Collectors.toList()); //获取学生的Id集合 List idList = list.stream().map(Student::getId).collect(Collectors.toList());
//获取姓名是ww的集合 List2.3 Sort 排序collect = list.stream().filter(student -> "ww".equals(student.getName())).collect(Collectors.toList());
//按照学生的年龄升序排列 List3. 常用操作 3.1 给list去重sortedUpStudent = list.stream().sorted(Comparator.comparingInt(Student::getAge)).collect(Collectors.toList()); //按照学生的年龄降序排列 List sortedDowmStudent = list.stream().sorted(Comparator.comparingInt(Student::getAge).reversed()).collect(Collectors.toList());
//根据学生的name属性去重 List3.2 Collectors相关实例hostList=list.stream().collect(Collectors .collectingAndThen(Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(Student :: getName))), ArrayList::new));
// 累积名字属性到一个集合中
List list = people.stream().map(Person::getName).collect(Collectors.toList());
// 累积名字属性到一个TreeSet中
Set set = people.stream().map(Person::getName).collect(Collectors.toCollection(TreeSet::new));
// 将元素转为String,并串联他们,中间的分隔符是逗号
String joined = things.stream()
.map(Object::toString)
.collect(Collectors.joining(", "));
// 见员工的工资求和
int total = employees.stream()
.collect(Collectors.summingInt(Employee::getSalary)));
// 将员工按部门分组
Map> byDept
= employees.stream()
.collect(Collectors.groupingBy(Employee::getDepartment));
// 求出各部门的薪资总和
Map totalByDept
= employees.stream()
.collect(Collectors.groupingBy(Employee::getDepartment,
Collectors.summingInt(Employee::getSalary)));
// 按照分数及格与否对学生分区
Map> passingFailing =
students.stream()
.collect(Collectors.partitioningBy(s -> s.getGrade() >= PASS_THRESHOLD));
4. 练习实例
// 测试数据
List list = new ArrayList<>();
list.add(new Student("李明", "男", 10D));
list.add(new Student("李飞", "男", 20d));
list.add(new Student("张丽", "女", 30d));
list.add(new Student("张艳", "女", 40d));
list.add(new Student("王朋", "男", 50d));
list.add(new Student("陈晴", "女", 60d));
list.add(new Student("王菲", "女", 70d));
list.add(new Student("李雷", "男", 80d));
list.add(new Student("张三", "中性", 80d));
list.add(new Student("李四", "中性", 80d));
要求返回格式为: (根据性别分组,且金额累加)
[{value: 1048, name: '中性'},{value: 735, name: '男'},{value: 735, name: '女'}]
解决方法:
List> collect = //流stream化 list.stream() //开始对性别分组和金额求和 .collect(Collectors.groupingBy(Student::getGender,Collectors.summingDouble(Student::getMoney))) //上面得到的是一个Map ,所以需要进一步转化 .entrySet() //流stream化 .stream() //执行中间操作map,把map转化成List



