在Java 8中按流从1个列表的1个列表中获取所有对象?

时间:2019-02-01 02:48:28

标签: java spring

我有2个课程,这是

public class TimeCardResponse {
   private String login;
   List<TimeCardDetail> timeCardDetails;
}

这:

public TimeCardDetail(String workingDay, Float workingTime) {
    this.workingDay = workingDay;
    this.workingTime = workingTime;
}
    input : a List<TimeCardResponse>

我想在workingDay中获得类TimeCardDetail的所有List<String>,并在Java 8中使用Stream

2 个答案:

答案 0 :(得分:0)

List<String> workingDays  = a.stream().map(TimeCardResponse::getTimeCardDetails)
                .filter(x-> Objects::nonNull)
                .flatMap(List::stream)
                .map(TimeCardDetail::getWorkingDay).collect(toList());

答案 1 :(得分:0)

假设您有一些getWorkingDay方法:

List<String> workDays = timeCardDetails.stream()
                                       .map(TimeCardDetail::getWorkingDay)
                                       .filter(Objects::nonNull)    //filter out null values
                                       .collect(Collectors.toList());

哪个会退回{@ {1}}的卡片工作时间