package LambdaTest.LambdaTest06; import java.util.ArrayList; /** * FileName: StreamDemo * Author: lps * Date: 2022/4/9 21:00 * Sign:刘品水 Q:1944900433 */ public class StreamDemo { public static void main(String[] args) { ArrayList<String> array = new ArrayList<>(); array.add("刘品水"); array.add("刘品火"); array.add("大张伟"); array.add("刘一手"); array.add("刘红"); array.add("张伟"); array.add("小张伟"); ArrayList<String> Liulist = new ArrayList<>(); for (String s : array){ if (s.startsWith("刘")){ Liulist.add(s); } } //System.out.println(Liulist); ArrayList<String> ThreeLiulist = new ArrayList<>(); for (String s:Liulist){ if (s.length()==3){ ThreeLiulist.add(s); } } //System.out.println(ThreeLiulist); for (String s:ThreeLiulist){ System.out.println(s); } System.out.println("=============="); //stream流来改进 //filter //Stream<T> filter(Predicate<? super T> predicate) //返回由该流的元素组成的流,该元素与给定的谓词匹配 这是一个intermediate operation。参数 // predicate - non-interfering, stateless谓词应用到每个元素以确定它是否应该包括 结果新的流 //array.stream().filter(s -> s.startsWith("刘")).filter(s -> s.length()==3).forEach(s -> System.out.println(s)); array.stream().filter(s -> s.startsWith("刘")).filter(s -> s.length()==3).forEach(System.out::println); } }