栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Java 8 Stream Lambda 的学习与使用

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Java 8 Stream Lambda 的学习与使用

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: '女'}]

解决方法:

List> collect = 
//流stream化
list.stream()
//开始对性别分组和金额求和
.collect(Collectors.groupingBy(Student::getGender,Collectors.summingDouble(Student::getMoney)))
//上面得到的是一个Map,所以需要进一步转化
.entrySet()
//流stream化
.stream()
//执行中间操作map,把map转化成List
.map(entry -> {
    HashMap hashMap = new HashMap<>();
      hashMap.put("name", entry.getValue());
      hashMap.put("value", entry.getKey());
      return hashMap;
})
//操作结束,返回List
.collect(Collectors.toList());

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/939627.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号