如何解决列表流HashMap的改进
有没有一种使用列表流来改进此方法的方法。
@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;
}
我已经尝试过,但是由于使用了lambda运算符,所以我无法使其正常工作。
return results.stream().collect(Collectors.toMap(Tuple::get(0,Tuple::get(1,Long.class));
Tuple::get(0,Long.class)
发出错误信号,但IDE无法识别它是什么。
解决方法
您使用的语法不正确,正确的语法应该是:
return results.stream()
.collect(Collectors.toMap(t -> t.get(0,Long.class),t -> t.get(1,Long.class));
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。