Java 如何将这些流式贴图关键帧从long转换为对象?
我有一个方法,它看起来像:Java 如何将这些流式贴图关键帧从long转换为对象?,java,lambda,java-8,java-stream,Java,Lambda,Java 8,Java Stream,我有一个方法,它看起来像: public Map<Long, List<ReferralDetailsDTO>> getWaiting() { return referralDao.findAll() .stream() .map(ReferralDetailsDTO::new) .collect(Collectors.groupingBy(ReferralDe
public Map<Long, List<ReferralDetailsDTO>> getWaiting() {
return referralDao.findAll()
.stream()
.map(ReferralDetailsDTO::new)
.collect(Collectors.groupingBy(ReferralDetailsDTO::getLocationId, Collectors.toList()));
}
}
publicmap getWaiting(){
返回referralDao.findAll()
.stream()
.map(参考详细信息到::新建)
.collect(Collectors.groupingBy(refereraldetailsdto::getLocationId,Collectors.toList());
}
}
它向我返回一个位置ID映射,以引用DetailsTo对象。但是,我想交换LocationDTO对象的位置ID
我会天真地想象这样的事情可能会奏效:
public Map<Long, List<ReferralDetailsDTO>> getWaiting() {
return referralDao.findAll()
.stream()
.map(ReferralDetailsDTO::new)
.collect(Collectors.groupingBy(locationDao.findById(ReferralDetailsDTO::getLocationId), Collectors.toList()));
}
publicmap getWaiting(){
返回referralDao.findAll()
.stream()
.map(参考详细信息到::新建)
.collect(Collectors.groupingBy(locationDao.findById(refereraldetailsdto::getLocationId),Collectors.toList());
}
显然,我在这里是因为它没有-Java抱怨findById方法期望的是一个长值,而不是方法引用。对于如何巧妙地解决这个问题,有什么建议吗?提前感谢。首先,将地图的键类型从Long更改为相关的类(是
LocationDTO
还是其他类?)
其次,在查找中使用lambda表达式而不是方法引用:
public Map<LocationDTO, List<ReferralDetailsDTO>> getWaiting() {
return referralDao.findAll()
.stream()
.map(ReferralDetailsDTO::new)
.collect(Collectors.groupingBy(r -> locationDao.findById(r.getLocationId()));
}
publicmap getWaiting(){
返回referralDao.findAll()
.stream()
.map(参考详细信息到::新建)
.collect(Collectors.groupingBy(r->locationDao.findById(r.getLocationId()));
}
无需调用groupingBy(…,toList())
。groupingBy
方法的一个参数(为管道中的数据提供键映射的参数)已经将同一键的值放入列表中。