Java 过滤scala中对象列表的列表属性

Java 过滤scala中对象列表的列表属性,java,list,scala,filter,seq,Java,List,Scala,Filter,Seq,我正在尝试过滤scala中对象列表的list属性。例如: 用java定义的类 public class Result{ private Collection<Person> persons= new ArrayList(); public Collection<Person> getPersons() {return this.persons;} } public class Person{ public String name } 更新 我想

我正在尝试过滤scala中对象列表的list属性。例如:

用java定义的类

public class Result{
    private Collection<Person> persons= new ArrayList();
    public Collection<Person> getPersons() {return this.persons;}
}

public class Person{
    public String name
}
更新
我想返回结果列表中的所有结果,其中包含要筛选的每个结果的名称属性。

您正在将每个结果映射到已筛选的人员集合。如果要将这些集合包装回结果(可能是过滤空结果??),则需要以下内容:

listOfResutlts.flatMap(res => {
   val filtered = res.getPersons.filter(_.name=="xx")
   if(filtered.isEmpty) None else Some(new Result(filtered))
}

请注意,您需要能够从一组新的人员中构造一个新结果。

您确定要获得一个列表[字符串]?就我所读到的,你应该得到一个列表[集合[人]]。
listOfResutlts.flatMap(res => {
   val filtered = res.getPersons.filter(_.name=="xx")
   if(filtered.isEmpty) None else Some(new Result(filtered))
}