【发布时间】:2015-11-26 15:16:11
【问题描述】:
我有自己的班级CheckIn,其属性day 为String,workingHours 为int,inProgress 为boolean:
public class CheckIn {
public int id;
public String day;
public int workingHours;
public boolean inProgress;
public CheckIn(String day, in hours, boolean inProgress) {
this.day = day;
this.workingHours = hours;
this.inProgress = inProgress;
}
}
我的系统中有条目列表,我需要这些条目的摘要并将它们与天分组并总结工作时间。没关系,我可以用 lambda 来实现,但是如果有任何 in 条目为真,那么我想将进度设置为真怎么办?
// Suppose this is the inputs
List<CheckIn> checkinsList = new ArrayList<>();
checkinsList.add(new CheckIn("26-11-2015",6,true));
checkinsList.add(new CheckIn("27-11-2015",6,false));
checkinsList.add(new CheckIn("26-11-2015",6,false));
checkinsList.add(new CheckIn("27-11-2015",4,false));
checkinsList.add(new CheckIn("26-11-2015",1,false));
checkinsList.add(new CheckIn("28-11-2015",6,false));
checkinsList.add(new CheckIn("28-11-2015",6,false));
checkinsList.add(new CheckIn("28-11-2015",6,true));
List<CheckIn> summary = new ArrayList<>();
checkinsList.stream().collect(Collectors.groupingBy(Function.identity(),
() -> new TreeMap<>(
Comparator.<CheckIn, String>comparing(entry -> entry.day)),
Collectors.summingInt(entry -> entry.duration))).forEach((e, sumTargetDuration) -> {
CheckIn entry = new CheckIn();
entry.day = e.day;
entry.duration = sumTargetDuration;
// Here my something like what I need?
entry.inProgress = e.inProgress;
summary.add(entry);
});
我需要 summary 列表包含(在这种情况下用于输入)在这 3 天内有 3 个项目:
我想要的结果是这样的:
- 第一项
"26-11-2015" , 13 , truetrue 因为有 1 天“26-11-2015”的项目为真。 - 第二条
"27-11-2015" , 10 , false - 第三条
"28-11-2015" , 18 , true
如果当天有任何条目有inProgress == true,我希望摘要带有inProgress true 是否适用于lambda?
【问题讨论】:
标签: java lambda java-8 java-stream collectors