使用Collectors.summingInt时如何获取自定义类型而不是Integer? [英] How to get a custom type instead of Integer when using Collectors.summingInt?

查看:5248
本文介绍了使用Collectors.summingInt时如何获取自定义类型而不是Integer?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在创建一个 Map< String,Map< LocalDate,Integer>> ,就像这样,整数表示秒:

I am currently creating a Map<String, Map<LocalDate, Integer>> like this, where the Integer represents seconds:

Map<String, Map<LocalDate, Integer>> map = stream.collect(Collectors.groupingBy(
            x -> x.getProject(),
            Collectors.groupingBy(
                x -> x.getDate(),
                Collectors.summingInt(t -> t.getDuration().toSecondOfDay())
            )
        ));

我怎样才能创建一个 Map< String,Map< LocalDate,Duration> ;>

推荐答案

要更改整数 Collectors.summingInt 持续时间,您只需要替换收集器 with:

To change that Integer from Collectors.summingInt to a Duration, you simply need to replace that Collector with:

Collectors.collectingAndThen(
    Collectors.summingInt(t -> t.getDuration().toSecondOfDay()),
    Duration::ofSeconds
)

这篇关于使用Collectors.summingInt时如何获取自定义类型而不是Integer?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆