有没有一种快速的方法可以对List< List<Double> >求平均值,所以我只有一个Double列表,其中每一列都是该列中所有原始列表的平均值?

最佳答案

使用Java 8,您可以编写类似这样的内容-尽管它并不短...

List<List<Double>> list = Arrays.asList(Arrays.asList(1d, 2d, 3d),
                                        Arrays.asList(2d, 3d, 4d, 5d));

//the largest list size
int maxSize = list.stream().mapToInt(List::size).max().orElse(0);

//for each index in the final list, we calculate the average across "valid" lists
//i.e. lists that are large enough
List<Double> averages = IntStream.range(0, maxSize)
                        .mapToObj(i -> list.stream()
                                            .filter(sublist -> sublist.size() > i)
                                            .mapToDouble(sublist -> sublist.get(i))
                                            .average().getAsDouble())
                        .collect(toList());

//prints averages = [1.5, 2.5, 3.5, 5.0]
System.out.println("averages = " + averages);

09-10 12:29
查看更多