Stream commonly used methods_filter

Common methods in the Stream stream _filter: used to filter the data in the Stream stream

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

Example:

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));
    }
}

Program demonstration:
Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_44664432/article/details/109150260