我想使用java 8流。我检查了其他帖子,我不明白为什么我的代码在下面导致错误。有人可以告诉我我的代码有什么问题?谢谢Java 8循环arraylist的对象和添加元素,如果条件验证
private OptList myFunction(OptList childOpts, Opt subOpt) {
OptList results = new OptList();
for (Opt o : childOpts) {
if ((subOpt.getOptPri() == null || subOpt.getOptPrices().isZeroPrice())
&& (o.getOptPrices() == null || o.getOptPrices().isZeroPrice()))
{
results.add(o);
} else
if (subOpt.getOptPrices() != null && o.getOptPrices() != null) {
if (subOpt.getOptPrices().getPrice(PriceType.MS).equals(o.getOptPrices().getPrice(PriceType.MS))
&& subOpt.getOptPrices().getPrice(PriceType.DISC).equals(o.getOptPrices().getPrice(PriceType.DISC))
&& subOpt.getOptPrices().getPrice(PriceType.INV).equals(o.getOptPrices().getPrice(PriceType.INV))
&& subOpt.getOptPrices().getPrice(PriceType.INV_DISC).equals(o.getOptPrices().getPrice(PriceType.INV_DISC)))
{
results.add(o);
}
}
}
return results;
}
private OptionList myFunction(OptList childOpts,
Opt subOpt) {
return childOpts.stream()
.filter(o -> (((subOpt.getOptPrices() == null || subOpt.getOptPrices().isZeroPrice()) && (o.getOptPrices() == null || o.getOptPrices().isZeroPrice()))
|| ((subOpt.getOptPrices() != null && o.getOptPrices() != null) && (subOpt.getOptPrices().getPrice(PriceType.MS).equals(o.getOptPrices().getPrice(PriceType.MS))
&& subOpt.getOptPrices().getPrice(PriceType.DISC).equals(o.getOptPrices().getPrice(PriceType.DISC))
&& subOpt.getOptPrices().getPrice(PriceType.INV).equals(o.getOptPrices().getPrice(PriceType.INV))
&& subOpt.getOptPrices().getPrice(PriceType.INV_DISC).equals(o.getOptPrices().getPrice(PriceType.INV_DISC))) )))
.collect(Collectors.toList());
}
并且堆栈跟踪或错误消息是...? – SomeJavaGuy
可以请你具体说明你的具体问题或错误。此外,这是一个BIG表达式,是一个调试噩梦。 –
错误是:Stream中的collect(java.util.stream.Collector <?super net.cds.data.model.Ioption,A,R>)不能应用于(java.util.stream.Collector,java。 util.List >)。我应该通过类似.collect(OptList(Collectors.toList()))来施放返回的列表吗? –