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

Example:

We have a User data table with id, name, age, gender data.

Now you have obtained an array of List<User> type that contains all the contents of the data table through getUsers(), and now you need to set the conditions, we only need the data whose age is 20 and gender is male.

The array can be obtained with stream, filter and collect

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

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

).collect(Collectors.toList());

illustrate:

getUsers() gets a List<User> type List array

stream() converts an array into stream data

filter() sets filter conditions

userInfo -> userInfo.getAge() == 20 && userInfo.getGender() == "Male" is a lambda expression, userInfo is a piece of User data in the List array

collect(Collectors.toList()) ends the stream and turns it into a List array.
 

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]

    }
}

Guess you like

Origin blog.csdn.net/m0_63270506/article/details/126053885