【问题标题】:How to collect filtered stream array output as list如何将过滤后的流数组输出收集为列表
【发布时间】:2018-06-14 10:31:19
【问题描述】:

我是 Java8 语法的新手,我们如何在过滤后将输出作为列表。 在我的例子中,过滤器返回一个数组。

在cmets中添加了,有没有更好的办法获取呢

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);

【问题讨论】:

    标签: java java-8 java-stream


    【解决方案1】:

    要检索List&lt;List&lt;T&gt;&gt;,可以按如下方式完成:

     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&lt;T&gt; 格式,请使用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() 等。

    【讨论】:

      【解决方案2】:

      这应该会给你想要的结果。首先filter 带有给定代码 VOUCHER 的促销代码。对于每个过滤的促销代码,您都有一组配置。我们得到它并将其展平以获得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());
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-07-22
        • 1970-01-01
        • 1970-01-01
        • 2019-01-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-02-20
        相关资源
        最近更新 更多