这个list元素是用;分割的字符串 将每个元素用;分割成字符串数组 并将每个数组将相同位置的数据相加求平均值

carlors / 2023-08-22 / 原文

public static void main(String[] args) {
List<String> list = new ArrayList<>();
list.add("2.0;2.2;4.0");
list.add("3.0;4.2;5.0");
list.add("4.0;5.2;3.0");

String[] firstRow = list.get(0).split(";");

List<String> collect = Arrays.stream(firstRow)
.map(i -> Arrays.stream(list.toArray(new String[0]))
.mapToDouble(row -> Double.parseDouble(row.split(";")[Arrays.asList(firstRow).indexOf(i)]))
.average()
.orElse(0.0))
.map(i -> String.format("%.1f", i))
.collect(Collectors.toList());

System.out.println(String.join(";", collect));
}