(1)
List<KfProduct> checkFileProducts=new ArrayList<KfProduct>();
checkFileProducts.stream().filter(m -> m.getId().equals(p.getId())).findAny().isPresent()
checkFileProducts.stream().filter(m -> m.getId().equals(p.getId())).findAny().orElse(null)
filter为过滤(m -> m.getId().equals(p.getId()))为过滤条件
.findAny()表示将其中任意一个返回
.isPresent()为了判断查询的类对象是否存在(.isPresent()一般与.get()方法合用 m.isPresent()如果存在则为true)
.orElse(null)表示如果一个都没找到返回null
(2)
List<KfProduct> allProducts = Stream.of(products,fileProducts).flatMap(Collection::stream).distinct().collect(Collectors.toList());
static Stream of(T… values)
参数:此方法接受强制参数值,这些参数值是新流的元素。
返回值:(T…values)的流返回其元素为指定值的顺序有序流。
例如:
Stream stream = Stream.of("Geeks", "for", "Geeks");
stream.forEach(System.out::println);
输出:
Geeks
for
Geeks
.flatMap()参考Java8 Stream使用flatMap合并List