英文:
Improvement with list stream HashMap
问题
使用列表流(list stream)来改进这个方法有没有办法。
@Override
public HashMap<Long, Long> countSearchBetweenDates(OffsetDateTime startPeriod, OffsetDateTime endPeriod) {
List<Tuple> results = this.repository.countBetweenDate(startPeriod, endPeriod);
HashMap<Long, Long> companyCountMap = results.stream()
.collect(Collectors.toMap(
t -> t.get(0, Long.class),
t -> t.get(1, Long.class)
));
return companyCountMap;
}
我尝试过,但是由于 lambda 操作符的原因,我就是无法让它工作。
return results.stream().collect(Collectors.toMap(
t -> t.get(0, Long.class),
t -> t.get(1, Long.class)
));
Tuple::get(0, Long.class), Tuple::get(1, Long.class)
被标记为错误,但是 IDE 无法识别出问题出在哪里。
英文:
Is there a way of improving this method using list stream.
@Override
public HashMap<Long, Long> countSearchBetweenDates(OffsetDateTime startPeriod, OffsetDateTime endPeriod) {
List<Tuple> results = this.repository.countBetweenDate(startPeriod, endPeriod);
HashMap<Long,Long> companyCountMap = new HashMap<>();
for(Tuple t : results){
companyCountMap.put(t.get(0,Long.class), t.get(1, Long.class));
}
return companyCountMap;
}
I've tried it but I just can't get it to work because of the lambda operator.
return results.stream().collect(Collectors.toMap(Tuple::get(0, Long.class), Tuple::get(1, Long.class));
The Tuple::get(0, Long.class), Tuple::get(1, Long.class)
gets signaled as an error but IDE can't identify what is it.
答案1
得分: 3
你正在使用的语法不正确,正确的语法应该是:
return results.stream()
.collect(Collectors.toMap(t -> t.get(0, Long.class), t -> t.get(1, Long.class)));
英文:
The syntax you are using is not correct, the correct one should be :
return results.stream()
.collect(Collectors.toMap(t -> t.get(0, Long.class), t -> t.get(1, Long.class));
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论