Java8 Stream流的合并

最近的需求里有这样一个场景,要校验一个集合中每个对象的多个Id的有效性。比如一个Customer对象,有3个Id:id1id2id3,要把这些Id全部取出来,然后去数据库里查询它们是否存在。

@Data
public class Customer {
    private String name;
    private String id1;
    private String id2;
    private String id3;
}

通常情况下,我们都是从集合中取出对象的某一个字段,像这样:

List<String> id1s =
    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

<R> Stream<R> flatMap(Function<? super T, ? extends Stream<? extends R>> mapper);

flatMap方法让你把一个流中的每个值都换成另一个流,然后把所有的流连接起来成为一个流。

Stream.flatMap方法的入参为一个Function函数,函数返回值的泛型要求为Stream类型。对比来看,mapflatMap都是将流中的元素映射为我们想要的值,只是flatMap把流中元素映射为一个新的Stream。

Stream.of(T... values)方法将多个元素构建成一个流,相当于Arrays.stream方法,元素本身为Stream时就可以构建一个泛型为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, stream, stream), 得到的结果就是Stream<Stream>,而flatMap要将元素映射为Stream,所以flatMap中的Lambda表达式返回本身即可,然后把多个流合成一个新流。

加深印象

再举一个例子,假设有这样一个需求:有一个由逗号连接的字符串组成的数组,如{"1,2,3", "3,4,5"},要求合并为一个数组,并去重,如{"1", "2", "3", "4", "5"}

public static void main(String[] args) {
    String[] strArray = {"1,2,3", "3,4,5"};
    List<String> collect = Arrays.stream(strArray) // Stream<String>
            .map(str -> str.split(",")) // Stream<String[]>
            .flatMap(Arrays::stream) // Stream<String>
            .distinct()
            .collect(Collectors.toList());
    System.out.println(collect);
}

map函数将元素映射为一个数组,flatMap函数再将元素映射为一个Stream,并将所有Stream合并成一个新Stream,然后就能愉快操作流中的每个元素了。

posted @ 2023-05-05 23:00  xfcoding  阅读(1255)  评论(0编辑  收藏  举报