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

举例:

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

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

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

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

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

).collect(Collectors.toList());

说明:

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

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

filter()设置过滤条件

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

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

package com.example.test3;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

public class test3 {
    public static void main(String[] args) {
        People people1 = new People(1, "Ligs", true);
        People people2 = new People(2, "Songzx", false);
        People people3 = new People(3, "Jinzg", true);
        People people4 = new People(4, "Liuzx", false);
        People people5 = new People(5, "Hedx", true);
        People people6 = new People(6, "Quansm", false);
        People people7 = new People(7, "Liangsz", true);
        People people8 = new People(8, "Chisz", true);

        ArrayList<People> list = new ArrayList<People>() {
   
   {
            add(people1);
            add(people2);
            add(people3);
            add(people4);
            add(people5);
            add(people6);
            add(people7);
            add(people8);
        }};
        System.out.println("======遍历=====");

        List<Boolean> collect = list.stream().map(People::getSex).collect(Collectors.toList());
        System.out.println("list.stream().map(People::getSex).collect(Collectors.toList() = "
                + collect); // [true, false, true, false, true, false, true, true]

        List<String> collect1 = list.stream().map(People::getName).collect(Collectors.toList());
        System.out.println("list.stream().map(People::getName).collect(Collectors.toList()) = " + collect1);
        //[Ligs, Songzx, Jinzg, Liuzx, Hedx, Quansm, Liangsz, Chisz]

        List<Integer> collect2 = list.stream().map(People::getId).collect(Collectors.toList());
        System.out.println("list.stream().map(People::getId).collect(Collectors.toList()) = "
                + collect2); //[1, 2, 3, 4, 5, 6, 7, 8]


        List<People> collect3 = list.stream().filter(People::getSex).collect(Collectors.toList());
        System.out.println("list.stream().filter(People::getSex).collect(Collectors.toList()) = "
                + collect3);//[com.example.test3.People@6433a2, com.example.test3.People@5910e440
        // , com.example.test3.People@6267c3bb, com.example.test3.People@533ddba
        // , com.example.test3.People@246b179d]


        List<People> collect4 = list.stream().filter(a -> a.getSex() == true && a.getId() == 1).collect(Collectors.toList());
        System.out.println("list.stream().filter(a -> a.getSex() == true && a.getId() == 1).collect(Collectors.toList()) = "
                + collect4);//[com.example.test3.People@6433a2]

    }
}

猜你喜欢

转载自blog.csdn.net/m0_63270506/article/details/126053885
xxx