1

Java8 Stream流的合并 - xfcoding

 1 year ago
source link: https://www.cnblogs.com/cloudrich/p/17375636.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

最近的需求里有这样一个场景,要校验一个集合中每个对象的多个Id的有效性。比如一个Customer对象,有3个Id:id1id2id3,要把这些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类型。对比一下,mapflatMap都是将流中的元素映射为我们想要的值,只是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<Stream>,紧接着用flatMap扁平化处理,把每一个元素合成一个新流。


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK