2017-04-11 86 views
0

我想使用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()); 
    } 
+1

并且堆栈跟踪或错误消息是...? – SomeJavaGuy

+1

可以请你具体说明你的具体问题或错误。此外,这是一个BIG表达式,是一个调试噩梦。 –

+0

错误是: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()))来施放返回的列表吗? –

回答

0

我明白,你有两个功能,一是利用定期收集操作来实施,而其他使用流实现的,你想知道为什么第二不起作用。

我不知道什么是OptList,但它不会是一个List<Opt>,这是什么collect(Collectors.toList())会返回。您需要某种方法才能使collect函数返回OptList

您有两种选择。

如果OptList是(延伸)Collection<Opt>,那么你可以使用toCollection并传递一个产生的OptList功能:

childOpts.stream(). (other stuff) .collect(Collectors.toCollection(OptList::new)) 

这工作,因为collect方法知道如何调用的的add方法产生Collection

如果OptList不是集合类型,那么您仍然可以使用collect,但是您必须build your own collector。您需要提供一个创建输出类型的函数,一个添加新成员的函数,以及一个将两个输出类型合并为一个的函数,如果您正在收集并行流,则会使用该函数。

这可能会是这个样子:

childOpts.stream(). (other stuff) .collect(OptList::new, OptList::add, OptList::addAll) 

如果OptList没有一个addAll,那么你可以尝试通过null(不知道这会工作!),或者通过抛出UnsupportedOperationException功能。

+0

非常感谢Willis BlackBurn。 OptionList扩展ArrayList

+0

好 - 你能接受答案吗(点击旁边的勾选按钮)? –

0

您正在使用自己的列表执行。 Collectors.toList()会返回标准集合类型:List<Opt>

因此你的第一选择是使用标准像

private List<yourType> myFunction(OptList childOpts, Opt subOpt) 

,或者如果OptionList实现List,那么你可以到你的对方付费的电话更改为:

Collectors.toCollection(OptionList::new) 

这个小例子提供了一些返回的类型和标准类型信息:

List<String> values = Arrays.asList("1", "2", "3"); 
System.out.println( 
    values.stream().collect(toList()) 
    .getClass().getTypeName()); 
System.out.println( 
    values.stream().collect(toCollection(LinkedList::new)) 
    .getClass().getTypeName()); 

它打印:

java.util.ArrayList 
java.util.LinkedList 
0

让我们来看看myFunction方法:

private OptionList myFunction(OptList childOpts, Opt subOpt) { 
    return childOpts.stream() 
      .filter(...) 
      .collect(Collectors.toList()); 
} 

它需要的Opt S(OptList),消除那些谁不符合特定标准,则组列表他们返回toList()收集器,该收集器返回流式传输项目的java.util.List

因此,表达式的返回类型(以及整个方法的返回类型)应该是List<Opt>或它的超类型(例如,Collection<Opt>)。

因此您当前的方法签名是错误的,编译器会抱怨。