汇总列表的每一列并将其添加到最后一项

时间:2018-10-26 04:31:15

标签: java

我的数据如下:

 List<T> mainList = {
    { ID = 001, Name = "AAA", List<String>totalTimeList = {"8", "0.45", "2"},
    { ID = 002, Name = "BBB", List<String>totalTimeList = {"10", "1.3", "2.2"},
    { ID = 003, Name = "CCC", List<String>totalTimeList = {"9", "1.3", "2.4"}
 }

注意。。totalTimeList是小时和分钟的列表。 (1.3 = 1小时30分钟。)
如何在“ mainList”上添加最后一项以总计“ totalTimeList”的各列?
结果输出:

 List<T> mainList = {
         { ID = 001, Name = "AAA", List<String>totalTimeList = {"8", "0.45", "2"},
         { ID = 002, Name = "BBB", List<String>totalTimeList = {"10", "1.3", "2.2"},
         { ID = 003, Name = "CCC", List<String>totalTimeList = {"9", "1.3", "2.4"},
         { ID = null, Name = null, List<String>totalTimeList = {"27", "3.45", "7"}
    }

可以为此使用java流吗?

有我的代码:

List<T> mainList = {
        { ID = 001, Name = "AAA", List<String>totalTimeList = {"8", "0.45", "2"},
        { ID = 002, Name = "BBB", List<String>totalTimeList = {"10", "1.3", "2.2"},
        { ID = 003, Name = "CCC", List<String>totalTimeList = {"9", "1.3", "2.4"}
     }
int sizeOftotalTimeList = mainList[0].getTotalTimeList.size();
List<String> lstSum = new ArrayList<>(Collections.nCopies(sizeOftotalTimeList , "0"));                          

for (T tmp: mainList ) {
  List<String> lstHr = tmp.getTotalTimeList();    
  for (int i = 0; i < lstHr.size(); i++) {
    int allMinute = 0;
    if(lstSum.size() > 0 && lstSum.size()-1 >= i) {
       Double sumOld = Double.valueOf(lstSum.get(i));                                        
       String[] part = String.format ("%.2f", sumOld).split("\\.");                                      
       allMinute = (Integer.parseInt(part[0])*60) + Integer.parseInt(part[1]);                                   
    }   
    Double number = Double.valueOf(lstHr.get(i));
    String[] separated = String.format ("%.2f", number).split("\\.");
    allMinute += (Integer.parseInt(separated[0])*60) + Integer.parseInt(separated[1]);  
    lstSum.set(i, convertToHoursMinutes(allMinute));
  }                             
}

T lastT = new T();
lastT.setTotalTimeList(lstSum);
mainList.add(lastT);

1 个答案:

答案 0 :(得分:0)

您可以这样做:-

List<Student> mainList = getMainList();
List<Double> newTimeList = Stream.of(0.0, 0.0, 0.0).collect(Collectors.toCollection(ArrayList::new));
List<Double> totalTimeLists = mainList.stream()
        .flatMap(student -> student.getTotalTimeList().stream())
        .map(Double::parseDouble)
        .collect(Collectors.toCollection(ArrayList::new));
for (int i = 0; i < totalTimeLists.size(); i++) {
    int pos = i % 3;
    //TODO apply mod if sum is greater than 59
    newTimeList.set(pos, newTimeList.get(pos) + totalTimeLists.get(i));
}
mainList.add(new Student(0, null,
        newTimeList.stream()
                .map(String::valueOf)
                .collect(Collectors.toCollection(ArrayList::new))));

我本可以将上述for循环转换为IntStream范围的那种循环,但是不建议这样做。可读性比尝试将所有内容更改为Stream / lambda / function样式更为重要。

我使用Student类来存储ID,名称,时间列表:-

public class Student {
    private int id;
    private String name;
    private List<String> totalTimeList;

    //constructor, getters/setters
}

PS-我使用toCollection(ArrayList::new)而不是toList(),因为列表的顺序在这里非常重要,并且toList()不保证将返回哪种类型的列表。