本文通过简单的例子向读者讲述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);
}
}