JAVA之List过滤

新建一个实体类

public class User {
    private Integer ID;
    private String name;
    private int age;
    private String birthday;

    public User(Integer ID, String name, int age, String birthday) {
        this.ID = ID;
        this.name = name;
        this.age = age;
        this.birthday = birthday;
    }

    public Integer getID() {
        return ID;
    }

    public void setID(Integer ID) {
        this.ID = ID;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getBirthday() {
        return birthday;
    }

    public void setBirthday(String birthday) {
        this.birthday = birthday;
    }

    @Override
    public String toString() {
        return "User{" +
                "ID=" + ID +
                ", name='" + name + '\'' +
                ", age=" + age +
                ", birthday='" + birthday + '\'' +
                '}';
    }
}

List过滤的三种方式:

  1. 通过java8中filter过滤器进行过滤
  2. 通过For循环遍历过滤
  3. 通过ForEach遍历过滤
public class FilteringList {

    /**
     * 通过java8中filter过滤器进行过滤
     * @param userList
     * @return
     */
    public List<User> filterByStream(List<User> userList){
        return userList.stream()
                .filter(user -> Objects.nonNull(user))
                .filter(user -> user.getAge()>=12)
                .collect(Collectors.toList());
    }

    /**
     * 通过For循环遍历过滤
     * @param userList
     * @return
     */
    public List<User> filterByFor(List<User> userList){
        List<User> newUserList = new ArrayList<>();
        for (User user : userList) {
            if(user != null && user.getAge()>=12){
                newUserList.add(user);
            }
        }
        return newUserList;
    }

    /**
     * 通过ForEach遍历过滤
     * @param userList
     * @return
     */
    public List<User> filterByForEach(List<User> userList){
        List<User> newUserList = new ArrayList<>();
        userList.forEach(user -> {
            if(user != null && user.getAge()>=12){
                newUserList.add(user);
            }
        });
        return newUserList;
    }
}

测试

public class TestFilteringList {
    public static List<User> userList;
    static {
        User zhangSan = new User(1,"张三",10,"2001-01-02");
        User liSi = new User(2,"李四",11,"2001-01-02");
        User wangWu = new User(3,"王五",12,"2003-01-02");
        User zhaoLiu = new User(4,"赵六",18,"2003-01-02");
        userList = Arrays.asList(zhangSan,liSi,wangWu,zhaoLiu);
    }

    @Test
    public void testFilterByStream(){
        FilteringList filteringList = new FilteringList();
        System.out.println("过滤前:" + userList);
        List<User> resUserList = filteringList.filterByStream(userList);
        System.out.println("过滤后:" + resUserList);
//        过滤前:
//        [User{ID=1, name='张三', age=10, birthday='2001-01-02'}, 
//        User{ID=2, name='李四', age=11, birthday='2001-01-02'}, 
//        User{ID=3, name='王五', age=12, birthday='2003-01-02'}, 
//        User{ID=4, name='赵六', age=18, birthday='2003-01-02'}]
//        过滤后:
//        [User{ID=3, name='王五', age=12, birthday='2003-01-02'}, 
//        User{ID=4, name='赵六', age=18, birthday='2003-01-02'}]

    }

    @Test
    public void testFilterByFor(){
        FilteringList filteringList = new FilteringList();
        System.out.println("过滤前:" + userList);
        List<User> resUserList = filteringList.filterByFor(userList);
        System.out.println("过滤后:" + resUserList);

//        过滤前:
//        [User{ID=1, name='张三', age=10, birthday='2001-01-02'}, 
//        User{ID=2, name='李四', age=11, birthday='2001-01-02'}, 
//        User{ID=3, name='王五', age=12, birthday='2003-01-02'}, 
//        User{ID=4, name='赵六', age=18, birthday='2003-01-02'}]
//        过滤后:
//        [User{ID=3, name='王五', age=12, birthday='2003-01-02'}, 
//        User{ID=4, name='赵六', age=18, birthday='2003-01-02'}]
    }

    @Test
    public void testFilterByForEach(){
        FilteringList filteringList = new FilteringList();
        System.out.println("过滤前:" + userList);
        List<User> resUserList = filteringList.filterByForEach(userList);
        System.out.println("过滤后:" + resUserList);
//        过滤前:
//        [User{ID=1, name='张三', age=10, birthday='2001-01-02'}, 
//        User{ID=2, name='李四', age=11, birthday='2001-01-02'}, 
//        User{ID=3, name='王五', age=12, birthday='2003-01-02'}, 
//        User{ID=4, name='赵六', age=18, birthday='2003-01-02'}]
//        过滤后:
//        [User{ID=3, name='王五', age=12, birthday='2003-01-02'}, 
//        User{ID=4, name='赵六', age=18, birthday='2003-01-02'}]
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值