Java,Lambda:如何从具有多个不同类型列表的类中选择列表?

Java,Lambda:如何从具有多个不同类型列表的类中选择列表?,java,lambda,filter,prediction,Java,Lambda,Filter,Prediction,在Java 8中,从对象(记录)列表中选择和收集列表的最佳方法是什么?对象(记录)列表有多个不同类型的列表{List,List,List,…} 类型1、类型2、类型3等彼此不相关。 T=类型1、类型2、类型3 List<Records> allRecords; class Records { List<Type1> listOfType1; // can be empty or null List<Type2> listOfType2; //

在Java 8中,从对象(记录)列表中选择和收集
列表的最佳方法是什么?对象(记录)列表有多个不同类型的列表
{List,List,List,…}

类型1、类型2、类型3等彼此不相关。 T=类型1、类型2、类型3

List<Records> allRecords;

class Records {
    List<Type1> listOfType1; // can be empty or null
    List<Type2> listOfType2; // can be empty or null
    List<Type3> listOfType3; // can be empty or null
}

List<T> getAllOccurrencesForType(T t, List<Records> allRecords) {
    return ?all occurrences of List<t> from all Records collected to one List? 
}
列出所有记录;
课堂记录{
List listOfType1;//可以为空或null
List listOfType2;//可以为空或null
List listOfType3;//可以为空或null
}
List getAllOccurrencesForType(T,列出所有记录){
返回?从收集的所有记录到一个列表中的所有列表事件?
}

我相信传递返回所需
列表的getter
函数可以:

static <T> List<T> getAllOccurrencesForType(Function<Records,List<T>> getter, List<Records> allRecords) {
    return allRecords.stream()
                     .flatMap(r->getter.apply(r).stream())
                     .collect(Collectors.toList());
}
输出:

[a, b, c, d]
[1, 2, 4, 3]
[1.1, 4.4, -3.3, 135.3]

如果您的代码是这样的,那么每种类型只出现一次,您可以使用record.getListOfType1()。也许你会将这些列表动态地添加到records类中?有什么区别?我同意@luk2302-看起来你又在问同样的问题了。也许我错了,但请解释为什么需要第二个问题,以及它与您以前问的有什么不同?!维塞林·大卫杜夫,谢谢你的评论。列表可以有多个记录,每个记录对每种类型都有一个列表。
@luk2302
@GhostCat
:感谢您的提示。我会尽快检查的。昨天我请我的一位同事尝试为这项任务找到一个解决方案,他在我开会时在我的电脑上工作。我猜他是用我的账户来发帖的。如果是这样,我将把这个标记为重复。再次感谢!你好,Eran,回答得很好,非常感谢:-)由于
r->getter,我得到了以下编译错误。从方法
getAllOccurrencesForType()中应用(r)
<代码>无法推断flatMap的类型参数(Function@ThomasMuller我发布的代码通过编译并在EclipseNeon上运行。您使用的是什么IDE?Eran,我使用的是EclipseOxy,JavaEEIDEforWebDevelopers@ThomasMuller您确定您使用的代码与我的答案中的代码完全相同吗?我发现Java 8错误消息通常会误导您。简单的打字错误可能会给您一条错误消息那不会帮你找到它。
@Eran
:谢谢你的时间。我将你的方法放入了以下类:
公共类实用程序{static List getAllOccurrencesForType(Function getter,List allRecords){return allRecords.stream().flatMap(r->getter.apply(r.stream()).collector.toList();}
class Records {
    List<String> listOfType1;
    List<Integer> listOfType2;
    List<Double> listOfType3;
    public Records (List<String> l1, List<Integer> l2, List<Double> l3) {
      listOfType1 = l1;
      listOfType2 = l2;
      listOfType3 = l3;
    }
    public List<String> getListOfType1() {
      return listOfType1;
    }
    public List<Integer> getListOfType2(){
      return listOfType2;
    }
    public List<Double> getListOfType3(){
      return listOfType3;
    }
}
 List<Records> recs = new ArrayList<> ();
 recs.add (new Records (Arrays.asList ("a","b"), Arrays.asList (1,2), Arrays.asList (1.1,4.4)));
 recs.add (new Records (Arrays.asList ("c","d"), Arrays.asList (4,3), Arrays.asList (-3.3,135.3)));
 List<String> allStrings = getAllOccurrencesForType(Records::getListOfType1,recs);
 List<Integer> allIntegers = getAllOccurrencesForType(Records::getListOfType2,recs);
 List<Double> allDoubles = getAllOccurrencesForType(Records::getListOfType3,recs);
 System.out.println (allStrings);
 System.out.println (allIntegers);
 System.out.println (allDoubles);
[a, b, c, d]
[1, 2, 4, 3]
[1.1, 4.4, -3.3, 135.3]