2 List过滤--filter

版权声明:看什么?6,你和我,走一波! https://blog.csdn.net/qq_31323797/article/details/89330298
package com.java8.list;

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

/**
 * @author gp6
 * @date 2019-04-15
 */
public class TestList {
    public static void main(String[] args) {
        List<Person> personList = new ArrayList<Person>() {{
            add(new Person(1, "小明", 11, 1));
            add(new Person(2, "小红", 12, 0));
            add(new Person(3, "小兰", 13, 0));
            add(new Person(4, "小强", 14, 0));
            add(new Person(5, "小咪", 10, 1));
            add(new Person(6, "小子", 11, 1));
            add(new Person(7, "小虾", 12, 1));
        }};
        
        // 查询性别为1的条数
        long total = personList.stream().filter((person) -> person.getSex().equals(1)).count();
        // 4
        System.out.println(total);

        // 查出性别为1的列表
        List<Person> personListFilter = personList.stream().filter(person -> person.getSex().equals(1)).collect(Collectors.toList());
        System.out.println(personListFilter);
    }
}

猜你喜欢

转载自blog.csdn.net/qq_31323797/article/details/89330298
0条评论
添加一条新回复