2017-03-29 42 views
2

我有一组对象。我想根据某些标准对其进行过滤,即符合条件的标准,我想执行一些操作&然后再做一些映射。无法解析方法'映射'Java 8 Lambda

Set<Tasks> v1; 
Map<V, Long> vC = v1.stream() 
       .filter(t -> someCondition(t)) 
       //for these filtered operations perform some action 
       .forEach(t -> t.performAction(summation(t)) 
       .map(t->summation(t)) 
       .collect(groupingBy(identity(), counting())); 

我在地图上得到一个错误无法解析方法'地图'。如果我删除forEach它的作品。我知道每一个都是终端操作,但我想不出替代方案。

回答

3

您可以使用peek操作来实现你想要的:

Map<V, Long> vC = v1.stream() 
    .filter(t -> someCondition(t)) 
    .peek(t -> t.performAction(summation(t)) 
    .map(t->summation(t)) 
    .collect(groupingBy(identity(), counting())); 
+2

......和[强制性链接](http://stackoverflow.com/q/33635717/2711488)。除此之外,这个代码计算'summation(t)'两次,这是一个替代解决方案大喊... – Holger