最近的需求里有这样一个场景,要校验一个集合中每个对象的多个Id的有效性。比如一个Customer对象,有3个Id:id1,id2,id3,要把这些Id全部取出来,然后去数据库里查询它是否存在。- @Data
- @AllArgsConstructor
- public class Customer {
- private String name;
- private String id1;
- private String id2;
- private String id3;
- }
复制代码 在通常情况下,我们要从集合中取出一个对象属性,很好办,用这个办法:- customerList.stream().map(Customer::getId1).filter(Objects::nonNull).collect(Collectors.toList())
复制代码 现在要取3个字段,怎么做呢?
Stream.concat
Stream接口中的静态方法concat,可以把两个流合成一个,我们取3个字段可以合并两次:- Stream<String> concat = Stream.concat(customerList.stream().map(Customer::getId1),
- customerList.stream().map(Customer::getId2));
- List<String> ids = Stream.concat(concat, customerList.stream().map(Customer::getId3))
- .filter(Objects::nonNull)
- .collect(Collectors.toList());
复制代码 取4个字段,就再继续合并。但是这种不够简洁,可以使用扁平化流flatMap。
flatMap
flatmap方法让你把一个流中的每个值都换成另一个流,然后把所有的流连接起来成为一个流。
Stream.flatMap方法的入参为一个Function函数,函数返回值的泛型要求为Stream类型。对比一下,map和flatMap都是将流中的元素映射为我们想要的值,只是flatMap映射的结果是一个新的Stream。
而Stream.of方法刚好可以构建一个类型为Stream的原始流,以供flatMap操作。- List<String> ids = Stream.of(customerList.stream().map(Customer::getId1),
- customerList.stream().map(Customer::getId2),
- customerList.stream().map(Customer::getId3))
- .flatMap(idStream -> idStream)
- .filter(Objects::nonNull)
- .collect(Collectors.toList());
复制代码 注意,Stream.of方法返回的流的泛型跟方法入参的类型是一样,上面的代码就相当于,Stream.of(stream, stream, stream), 得到的结果就是Stream,紧接着用flatMap扁平化处理,把每一个元素合成一个新流。
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作! |