• java stream流 部分使用心得


    这次任务接触到了stream这个方法,感觉还是很实用的,借此记录一下使用。

    1 stream.map

     

    commercialTenantStores集合,我想获取集合中的某个值,就可以用map,重新过滤做成一个新的集合。

     当然,ideal也很人性化的自动提示。

    获取某个值,还可以lamda表达式获取。例如,這種形式。

    2 stream.filter

    过滤

    这里我用到的是,取两个集合的交集部分,按照以往我会写双重for循环,费时费力。

    现在是要过滤就能实现。

    filter()里面写你想要得到的结果,过滤掉你不要的结果。

     两个集合,我想取相同的值。就可以用 contains 方法,取不同的前面加个 !就行。

     2 stream.多个条件并用。

     当熟练掌握的时候,可以当个并用,提高开发效率。

    4 stream.

     这里面的方法特别多,我只用到几个,等以后用的时候,再做记录。

      8.31 号 新增 

    4.1 求和 

    有两种方法

     4.2 求平均

      4.3 分页 skip().limit()

     stream.skip().limit() 搭配使用

      类似 sql里的 limit   A, B

      skip(A).limit(B) 

      4.4排序   sorted(Comparator.comparing())

    倒序

     sorted(Comparator.comparing()).reversed()

    正序

     sorted(Comparator.comparing())

       4.5 分组 Collectors.groupingBy

       4.6 配合optional方法   stream 两个集合 相等赋值

    9.15 当然也可以写,两个foreach 类似双重for 循环 ,但是显得不够有逼格。

     4.7 stram.min  max  取集合中某个字段的最大值和最小值 

    4.8 .flatMap  扁平化map

    stream.Map api 是对集合进行操作,

    stream.fltaMap 是对集合里面的集合进行操作。(个人理解),把所有的集合全部扁平化。

    这里提供两个写法:

    测试类:

     public static void main(String[] args) {
            List<User> userList= new ArrayList<>();
            User user = new User();
            user.setId(1);
            user.setName("测试1");
            userList.add(user);
            User user1 = new User();
            user1.setId(2);
            user1.setName("测试2");
            userList.add(user1);
    
    
            List<people> peopleList= new ArrayList<>();
            people p = new people();
            p.setCity("芜湖");
            p.setCityid(101);
            peopleList.add(p);
            people p1 = new people();
            p1.setCity("虹口");
            p1.setCityid(103);
            peopleList.add(p1);
            user.setPeople(peopleList);
            user1.setPeople(peopleList);
    
            List<people> collect = userList.stream().flatMap(c -> c.getPeople().stream()).distinct().collect(Collectors.toList());
            List<people> people = userList.stream().map(User::getPeople).flatMap(Collection::stream).collect(Collectors.toList());
    

      这里的  user 是一个集合  里面 还嵌套一个 people 集合

    这时候 我们需要子集合的内容,从外层集合取里层集合的内容,就用到了flatmap方法。

     可以看出,两种写法的结果都是一致的。

            User user = new User();
            List<User> users = new ArrayList<>();
            users.add(user);
            User user2 = new User();
            user2.setId(1);
            user.setId(2);
            user2.setAge(12);
            user.setAge(13);
            users.add(user2);
    
            List<AdminUser> adminUserList = new ArrayList<>();
            AdminUser adminUser = new AdminUser();
            adminUser.setId(1);
            AdminUser adminUser1 = new AdminUser();
            adminUser1.setId(2);
            adminUserList.add(adminUser);
            adminUserList.add(adminUser1);
            System.out.println(adminUserList);
    
            adminUserList.stream().forEach(a->{
                Optional<User> optional = users.stream().filter(r ->r.getId().equals(a.getId())).findFirst();
                if(optional.isPresent()){
                    a.setAge(optional.get().getAge());
                }
            });
    
            System.out.println(adminUserList);
    
        }

    待续。。。

  • 相关阅读:
    端午节习俗
    彩绘漂亮MM集
    高效使用数码相机的诀窍
    项目管理缩略语
    数码相机如何当做摄像头(文字版)
    标签式按纽
    显示一个Form中的所有内容
    生活中要常常鼓励别人
    青苹果论坛重新开放
    第一次爱的人
  • 原文地址:https://www.cnblogs.com/zq1003/p/14291761.html
Copyright © 2020-2023  润新知