• 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. }

  • 相关阅读:
    ros2与windows入门教程-创建工作空间和功能包
    个人玩航拍,如何申请无人机空域?
    【Maven学习】3.3 实验三:执行 Maven 的构建命令
    JAVA设计模式-备忘录模式
    项目踩坑之面试遇到的问题及解决
    SparkSQL
    农夫山泉Java开发工程师一面经历
    华为云云耀云服务器L实例评测 | 开启OPC UA之旅
    Flask框架:如何运用Ajax轮询动态绘图
    python3.7 编译cv_bridge 踩坑记录
  • 原文地址:https://blog.csdn.net/m0_63270506/article/details/126053885