如何将筛选的流数组输出收集为列表

时间:2018-06-14 10:31:20

标签: java java-8 java-stream

我是Java8语法的新手,如何在过滤后将输出作为列表获取。 在我的情况下,过滤器返回一个数组。

在评论中添加,是否有更好的方法可以获得它

Config config = new Config("ooo", "wc", "code");
    Config config1 = new Config("ppp", "wc", "code");
    Config config2 = new Config("ooo", "wc", "code");

    Config[] configs = {config, config1, config2};

    Config config4 = new Config("ooo", "REG", "code");
    Config config5 = new Config("ppp", "REG", "code");
    Config config6 = new Config("ooo", "REG", "code");

    Config[] _configs = {config4, config5, config6};

    PromoCode promoCode = new PromoCode(121, "VOUCHER", "121", configs);
    PromoCode promoCode1 = new PromoCode(122, "VOUCHER", "122", null);
    PromoCode promoCode2 = new PromoCode(123, "LINK", "123", configs);
    PromoCode promoCode3 = new PromoCode(124, "VOUCHER", "124", null);
    PromoCode promoCode4 = new PromoCode(125, "LINK", "125", _configs);
    PromoCode promoCode5 = new PromoCode(126, "LINK", "126", _configs);

    List<String> resultantValues = new ArrayList<String>();
    PromoCode[] promoCodes = {promoCode, promoCode1, promoCode2, promoCode3, promoCode4, promoCode5};
    Stream<PromoCode> stream = Stream.of(promoCodes);
    stream.parallel()
        .filter(x -> x.getCode().equalsIgnoreCase("VOUCHER"))
        .collect(Collectors.toList())
        .parallelStream()
        .forEach(x-> {
            Stream.of(x.getConfigs())
                .filter(t -> t.getOccasion().equals("wc"))
            //after filter, how can we get the output
             // List of list of strings format
                .forEach(o -> {
                    resultantValues.add(o.getProduct()+"_"+o.getProduct());
                });
        });

    System.out.println(resultantValues);

2 个答案:

答案 0 :(得分:3)

要检索List<List<T>>,可按以下步骤操作:

 Stream.of(promoCodes)
       .parallel() // is this really needed?
       .filter(x -> x.getCode().equalsIgnoreCase("VOUCHER"))          
       .map(x-> 
            Stream.of(x.getConfigs())
                .filter(t -> t.getOccasion().equals("wc"))
                .map(o ->  o.getProduct()+"_"+o.getProduct())
                .collect(Collectors.toList())
       )
       .collect(Collectors.toList());

或者如果您想要List<T>格式,请使用flatMap

 Stream.of(promoCodes)
       .parallel() // is this really needed?
       .filter(x -> x.getCode().equalsIgnoreCase("VOUCHER"))          
       .flatMap(x-> 
            Stream.of(x.getConfigs())
                  .filter(t -> t.getOccasion().equals("wc"))
                  .map(o ->  o.getProduct()+"_"+o.getProduct())
        )
        .collect(Collectors.toList());

或@Holger提到,对于第二种方法,您可以避免在flatMap中嵌套:

 Stream.of(promoCodes)
       .parallel() // is this really needed?
       .filter(x -> x.getCode().equalsIgnoreCase("VOUCHER"))
       .flatMap(x -> Arrays.stream(x.getConfigs()))
       .map(x -> x.getProduct() + "_" + x.getProduct())
       .collect(Collectors.toList());

这绝对更具可读性:

请注意,我还删除了一些不必要的方法调用,例如中间收集到列表.collect(Collectors.toList()).parallelStream()等。

答案 1 :(得分:0)

这应该会给你想要的结果。使用给定代码VOUCHER的第一个filter promocodes。对于每个过滤的promocode,您有一组配置。我们得到它并将其展平以获得streamConfig个对象。在下一步中,我们过滤掉场合不等于wc的所有配置。然后我们映射所有匹配的config对象以获得所需的结果。在最后一步,我们将collect结果放入容器中。

final List<String> finalResult = Stream.of(promoCodes)
        .filter(pc -> pc.getCode().equalsIgnoreCase("VOUCHER"))
        .flatMap(pc -> Stream.of(pc.getConfigs()))
        .filter(conf -> conf.getOccasion().equals("wc"))
        .map(conf -> conf.getProduct() + "_" + conf.getProduct())
        .collect(Collectors.toList());