• list.stream().filter(a ->xxx ).collect(Collectors.toList())


    举例:

    我们有一个User数据表,里面有id,name,age,gender数据。

    现在已经通过getUsers()得到一个包含数据表全部内容的List类型的数组,现在需要设置条件,我们只要age为20并且gender为男的数据。

    可以用stream、filter以及collect获得该数组

    List users = getUsers().stream().filter(userInfo ->

            userInfo.getAge() == 20 && userInfo.getGender() == "男"

    ).collect(Collectors.toList());

    说明:

    getUsers()获得一个List类型的List数组

    stream()将数组转化为流数据

    filter()设置过滤条件

    userInfo -> userInfo.getAge() == 20 && userInfo.getGender() == "男"为lambda表达式,userInfo是List数组中一条User数据

    collect(Collectors.toList())结束流转为List数组。
     

    1. package com.example.test3;
    2. import java.util.ArrayList;
    3. import java.util.List;
    4. import java.util.stream.Collectors;
    5. public class test3 {
    6. public static void main(String[] args) {
    7. People people1 = new People(1, "Ligs", true);
    8. People people2 = new People(2, "Songzx", false);
    9. People people3 = new People(3, "Jinzg", true);
    10. People people4 = new People(4, "Liuzx", false);
    11. People people5 = new People(5, "Hedx", true);
    12. People people6 = new People(6, "Quansm", false);
    13. People people7 = new People(7, "Liangsz", true);
    14. People people8 = new People(8, "Chisz", true);
    15. ArrayList list = new ArrayList() {{
    16. add(people1);
    17. add(people2);
    18. add(people3);
    19. add(people4);
    20. add(people5);
    21. add(people6);
    22. add(people7);
    23. add(people8);
    24. }};
    25. System.out.println("======遍历=====");
    26. List collect = list.stream().map(People::getSex).collect(Collectors.toList());
    27. System.out.println("list.stream().map(People::getSex).collect(Collectors.toList() = "
    28. + collect); // [true, false, true, false, true, false, true, true]
    29. List collect1 = list.stream().map(People::getName).collect(Collectors.toList());
    30. System.out.println("list.stream().map(People::getName).collect(Collectors.toList()) = " + collect1);
    31. //[Ligs, Songzx, Jinzg, Liuzx, Hedx, Quansm, Liangsz, Chisz]
    32. List collect2 = list.stream().map(People::getId).collect(Collectors.toList());
    33. System.out.println("list.stream().map(People::getId).collect(Collectors.toList()) = "
    34. + collect2); //[1, 2, 3, 4, 5, 6, 7, 8]
    35. List collect3 = list.stream().filter(People::getSex).collect(Collectors.toList());
    36. System.out.println("list.stream().filter(People::getSex).collect(Collectors.toList()) = "
    37. + collect3);//[com.example.test3.People@6433a2, com.example.test3.People@5910e440
    38. // , com.example.test3.People@6267c3bb, com.example.test3.People@533ddba
    39. // , com.example.test3.People@246b179d]
    40. List collect4 = list.stream().filter(a -> a.getSex() == true && a.getId() == 1).collect(Collectors.toList());
    41. System.out.println("list.stream().filter(a -> a.getSex() == true && a.getId() == 1).collect(Collectors.toList()) = "
    42. + collect4);//[com.example.test3.People@6433a2]
    43. }
    44. }

  • 相关阅读:
    Java面试题(持续更新)
    LeetCode 36. 有效的数独
    草坪维护不求人,高尔夫球场草坪自动化灌溉联网方案
    JDBC数据库事务
    金属五要素微型气象仪科技小物大智慧
    IDEA实现命令行传参快捷方法传参
    【MySQL】多表查询
    功能出新|酷雷曼3D漫游,更沉浸的三维空间体验
    Angular实现日期,数据和更新
    企业电子招标采购系统源码Spring Boot + Mybatis + Redis + Layui + 前后端分离 构建企业电子招采平台之立项流程图
  • 原文地址:https://blog.csdn.net/m0_63270506/article/details/126053885