Java 如何将过滤后的流数组输出收集为列表
我不熟悉Java8语法,我们如何在过滤后得到列表形式的输出。 在我的例子中,filter返回一个数组 加上评论,有没有更好的办法Java 如何将过滤后的流数组输出收集为列表,java,java-8,java-stream,Java,Java 8,Java Stream,我不熟悉Java8语法,我们如何在过滤后得到列表形式的输出。 在我的例子中,filter返回一个数组 加上评论,有没有更好的办法 Config config = new Config("ooo", "wc", "code"); Config config1 = new Config("ppp", "wc", "code"); Config config2 = new Config("ooo", "wc", "code"); Config[] configs = {con
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);
Config-Config=新配置(“ooo”、“wc”、“code”);
配置1=新配置(“ppp”、“wc”、“代码”);
配置2=新配置(“ooo”、“wc”、“代码”);
Config[]configs={Config,config1,config2};
配置4=新配置(“ooo”、“REG”、“代码”);
配置配置5=新配置(“ppp”、“注册”、“代码”);
Config Config 6=新配置(“ooo”、“REG”、“代码”);
Config[]_configs={config4,config5,config6};
PromoCode PromoCode=新的PromoCode(121,“凭证”,“121”,配置);
PromoCode promoCode1=新的PromoCode(122,“凭证”,“122”,空);
PromoCode promoCode2=新的PromoCode(123,“链接”,“123”,配置);
PromoCode promoCode3=新的PromoCode(124,“凭证”,“124”,空);
PromoCode promoCode4=新的PromoCode(125,“链接”,“125”,“配置”);
PromoCode promoCode5=新的PromoCode(126,“链接”,“126”,“配置”);
List resultantValues=new ArrayList();
PromoCode[]promoCodes={PromoCode,promoCode1,promoCode2,promoCode3,promoCode4,promoCode5};
Stream-Stream=Stream.of(促销代码);
stream.parallel()
.filter(x->x.getCode().equalsIgnoreCase(“凭证”))
.collect(收集器.toList())
.parallelStream()
.forEach(x->{
Stream.of(x.getConfigs())
.filter(t->t.getOcement().equals(“wc”))
//经过筛选后,我们如何获得输出
//字符串列表格式
.forEach(o->{
add(o.getProduct()+“”+o.getProduct());
});
});
System.out.println(结果值);
要检索列表
,可以按如下操作:
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());
或者,如果需要列表
格式,则使用平面图
:
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()
等。这应该会给您期望的结果。首先过滤带有给定代码凭证的促销代码。对于每个筛选的promocode,您都有一个配置数组。我们得到它并将其展平,得到Config
对象的流。在下一步中,我们过滤掉所有时机不等于wc
的配置。然后我们映射所有匹配的config
对象以获得所需的结果。在最后一步,我们将结果收集到一个容器中
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());
finalResult=Stream.of(promoCodes)
.filter(pc->pc.getCode().equalsIgnoreCase(“凭证”))
.flatMap(pc->Stream.of(pc.getConfigs()))
.filter(conf->conf.getOcement().equals(“wc”))
.map(conf->conf.getProduct()
.collect(Collectors.toList());