Reputation: 775
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.
Upvotes: 1
Views: 71
Reputation: 60016
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));
Upvotes: 3