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]

    }
}

  • 8
    点赞
  • 36
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

91科技

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值