java8 stream filter()

本文通过简单的例子向读者讲述filter过滤的使用collect讲收集过滤的结果返回demo

package com.Cz.smile;

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

public class Java8Demo {

    public static void main(String[] args) {

        List<String> list= Arrays.asList("A","B","B","C","Y");

        //Java8 之前
        List<String> result=new ArrayList<>();
        for (String str: list) {
            if("B".equals(str)){
                result.add(str);
            }
        }
        System.out.println(result);

        //Java8
        List<String> demo= list.stream().filter(e->"B".equals(e)).collect(Collectors.toList());
        System.out.println(demo);

    }
}






    原文作者:Cz-smile
    原文地址: https://blog.csdn.net/smile_6688/article/details/78011576
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞