【发布时间】:2019-01-08 06:14:48
【问题描述】:
public static Set<NurseViewPrescriptionWrapper> create(final Set<NurseViewPrescriptionDTO> nurseViewPrescriptionDTOs) {
return nurseViewPrescriptionDTOs.stream()
.map(new Function<NurseViewPrescriptionDTO, NurseViewPrescriptionWrapper>() {
@Override
public NurseViewPrescriptionWrapper apply(NurseViewPrescriptionDTO input) {
return new NurseViewPrescriptionWrapper(input);
}
})
.collect(Collectors.toSet());
}
我将上面的代码转换为 java 8 lamda 函数,如下所示。
public static Set<NurseViewPrescriptionWrapper> create(final Set<NurseViewPrescriptionDTO> nurseViewPrescriptionDTOs) {
return nurseViewPrescriptionDTOs.stream()
.map(input -> new NurseViewPrescriptionWrapper(input))
.collect(Collectors.toSet());
}
现在,我收到声纳问题,例如 Lambdas should be replaced with method references ,以 '->' 这个符号。我该如何解决这个问题?
【问题讨论】:
-
简单的方法是定义一个将
NurseViewPrescriptionDTO作为参数并将其转换为NurseViewPrescriptionWrapper的方法。喜欢private NurseViewPrescriptionWrapper convert(NurseViewPrescriptionDTO d){return new NurseViewPrescriptionWrapper(d);}然后在... map(YOURCALSS::convert) ...中使用
标签: java lambda java-8 method-reference