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

  • 相关阅读:
    nios ii开发随笔
    〖Python 数据库开发实战 - MySQL篇⑫〗- 数据表的字段约束
    数据库等值查询与统计信息
    优雅的记录日志,拒绝打印模糊信息导致bug定位难
    再想一想GPT
    【软件测试笔试题】阿里巴巴(中国)网络技术有限公司
    转置卷积详解(原理+实验)
    基于HOG特征提取和GRNN神经网络的人脸表情识别算法matlab仿真,测试使用JAFFE表情数据库
    关于smartforms 文本乱码问题
    线程崩溃为什么不会导致 JVM 崩溃
  • 原文地址:https://blog.csdn.net/m0_63270506/article/details/126053885