【发布时间】:2014-04-16 08:23:00
【问题描述】:
假设有一个来自Target 类的对象和一组来自FriendOfTarget 类的对象,例如
public class Target{
private List<FriendOfTarget> completeListOfFriendOfTargets;
public boolean matchSomething(FriendOfTarget fot){
//do something by comparing the fields of Target and FriendOfTarget
}
public List<FriendOfTarget> reduce(){
//
}
}
public class FriendOfTarget{
//
}
如果我想构造一个 FriendOfTarget 对象列表,在 Java7 中,我在类 Target 的方法 reduceByParam 中执行以下操作:
public List<FriendOfTarget> reduce(){
List<FriendOfTarget> lst= new ArrayList<>();
for(FriendOfTarget fot: completeListOfFriendOfTargets){
if(reduceByThis(fot))
lst.add(fot);
}
return lst;
}
如何在 Java8 中做同样的事情?我很难找到有关如何将this 引用到stream 的anyMatch 方法的示例。
【问题讨论】:
-
第二个列表中是否有错字,应该是 reduceByParam 而不是 matchSomething。我没有看到任何与 anyMatch 相关的代码以及 this 存在问题的地方。
-
是的,你是对的!请,您的荣幸:编辑我的问题!
标签: java list java-8 java-stream