如何将参数发送到流中的引用方法(java 8)? [英] How to send parameters to a reference method in a stream (java 8)?

查看:111
本文介绍了如何将参数发送到流中的引用方法(java 8)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个活动列表(活动),我想确定一个数据结构 Map(String,DateTime)(不是持续时间或期间; <$映射的c $ c> DateTime 这是必须的)。对于每个活动,在监测期内计算的总持续时间。

I have a list of activities(Activity) and I want to determine a data structure of the form Map(String, DateTime) (not Duration or Period; DateTime it's a must) that maps. For each activity the total duration computed over the monitoring period.

活动类具有: activityLabel(String) startTime(DateTime) endTime(DateTime)。我使用 joda 时间。
这就是我所做的:

The class Activity has: activityLabel(String), startTime(DateTime), endTime(DateTime). I use joda time. This is what I have done:

Map<String, DateTime> durations = activities.stream().collect(Collectors.toMap(
                                  it -> it.activityLabel,
                                  it ->new DateTime(0,0,0,0,0,0)
                                 //,DateTime::plus
                                 ));

我想我应该使用 DateTime plus(ReadablePeriod period) DateTime plus(ReadableDuration duration),但我不知道如何将类型为Duration或Period的参数发送到方法引用。

I guess I should use DateTime plus(ReadablePeriod period) or DateTime plus(ReadableDuration duration) , but I don't know how to send a parameter of type Duration or Period to the method reference.

如何实现这一结果?

编辑:输入:

2011-12-03 01:00:00 2011-12-03 9:00:00睡觉

2011-12-03 01:00:00 2011-12-03 9:00:00 Sleeping

2011-12-04 03 :00:00 2011-12-04 10:30:00睡觉

2011-12-04 03:00:00 2011-12-04 10:30:00 Sleeping

我应该有输出:睡觉0-0-0 15:30:00(年,月,日,小时,分钟,秒)

I should have the output: Sleeping 0-0-0 15:30:00 (years,months,days,hours,minutes,seconds)

推荐答案

代码(使用句号)如下所示:

The code (using a Period) would look like this:

 Map<String, Period> map = activities.stream()
            .collect(Collectors.toMap(Activity::activityLabel, ac -> new Period(ac.getStartTime(), ac.getEndTime()),
                    (left, right) -> left.plus(right)));

如果你真的想输出期间作为字符串,您需要 PeriodFormatter

If you really want to output that Period as a String, you need PeriodFormatter.

 private static PeriodFormatter periodFormatter() {
    return new PeriodFormatterBuilder()
            .printZeroAlways()
            .minimumPrintedDigits(2)
            .appendYears().appendSeparator("-")
            .appendMonths().appendSeparator("-")
            .appendDays().appendLiteral(" ")
            .appendHours().appendSeparator(":")
            .appendMinutes().appendSeparator(":")
            .appendSeconds().toFormatter();
}

然后你的代码会更像这样:

And then your code would look more like this:

 Map<String, String> map = activities.stream()
            .collect(Collectors.collectingAndThen(
                    Collectors.toMap(
                            Activity::getLabel,
                            ac -> new Period(ac.getStartTime(), ac.getEndTime()),
                            Period::plus),
                    m -> m.entrySet().stream().collect(Collectors.toMap(
                            Entry::getKey,
                            e -> e.getValue().toString(periodFormatter)))));

    System.out.println(map);  // {Sleeping=00-00-00 15:30:00

这篇关于如何将参数发送到流中的引用方法(java 8)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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