Stream流常用方法_filter

Stream流中的常用方法_filter:用于对Stream流中的数据进行过滤

  Stream<T> filter(Predicate<? super T> predicate);
filter方法的参数Predicate是一个函数式接口,所以可以传递Lambda表达式,
对数据进行过滤
Predicate中的抽象方法:
    boolean test(T t);

示例:

public class Demo03Stream_filter {
    
    
    public static void main(String[] args) {
    
    
        //创建一个Stream流
        Stream<String> stream = Stream.of("张三丰", "张翠山", "赵敏", "周芷若", "张无忌");
        //对Stream流中的元素进行过滤,只要姓张的人
        Stream<String> stream2 = stream.filter(name -> name.startsWith("张"));
        //遍历stream2流
        stream2.forEach(name-> System.out.println(name));
    }
}

程序演示:
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/weixin_44664432/article/details/109150260