package com.chunzhi.Test02Stream;
import java.util.stream.Stream;
/*
Stream流中的常用方法filter:用于对Stream流中的数据进行过滤
Stream<T> filter(Predicate<? super T> predicate);
filter方法的参数Predicate是一个函数式接口,所以可以传递Lambda表达式,对数据进行过滤
Predicate中的抽象方法:
boolean test(T t);
*/
public class Test03Stream_filter {
public static void main(String[] args) {
// 创建Stream流
Stream<String> stream = Stream.of("张无忌", "周芷诺", "赵敏", "张强");
// 对Stream流中的元素进行过滤,只要姓张的人
Stream<String> stream01 = stream.filter((String name) -> {
return name.startsWith("张");
});
// 遍历Stream01流
stream01.forEach((String name) -> {
System.out.println(name);
});
// 可以使用Lambda表达式进行优化,在此尚不做演示
}
}