简体   繁体   中英

Improvement with list stream HashMap

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.

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));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM