问题
Today I started learning Java 8, so I'm quite new to Java 8 and it's stuff. I have a list of activities. An activity has name, startTime, endTime. For startTime and endTime i'm using DateTime from joda time.
I'm trying to determine a data structure of the form Map (String, DateTime) that maps for each activity the total duration computed over the monitoring period.
Here is a snippet of my code:
import java.util.ArrayList;
import java.util.Map;
import org.joda.time.DateTime;
class Activity {
public DateTime startTime;
public DateTime endTime;
public String activityLabel;
public Activity(DateTime startTime, DateTime endTime, String activityLabel) {
super();
this.startTime = startTime;
this.endTime = endTime;
this.activityLabel = activityLabel;
}
}
public class Main {
public static void main(String[] args) {
ArrayList<Activity> list = new ArrayList<>();
//adding activities to list
list.add(new Activity(new DateTime(2017, 5, 12, 13, 10, 0), new DateTime(2017, 5, 12, 13, 40, 0), "Showering"));
list.add(new Activity(new DateTime(2017, 5, 12, 13, 41, 0), new DateTime(2017, 5, 12, 14, 20, 0), "Cooking"));
list.add(new Activity(new DateTime(2017, 5, 12, 14, 20, 0), new DateTime(2017, 5, 12, 14, 50, 0), "Eating"));
list.add(new Activity(new DateTime(2017, 5, 12, 14, 51, 0), new DateTime(2017, 5, 12, 15, 30, 0), "Sleeping"));
list.add(new Activity(new DateTime(2017, 5, 12, 17, 22, 0), new DateTime(2017, 5, 12, 17, 30, 0), "Showering"));
list.add(new Activity(new DateTime(2017, 5, 12, 17, 41, 0), new DateTime(2017, 5, 12, 17, 50, 0), "Cooking"));
list.add(new Activity(new DateTime(2017, 5, 12, 17, 50, 0), new DateTime(2017, 5, 12, 18, 20, 0), "Eating"));
list.add(new Activity(new DateTime(2017, 5, 12, 18, 21, 0), new DateTime(2017, 5, 12, 20, 30, 0), "TV"));
//creating a stream from list
Map<String, DateTime> map;
// map = list.stream().collect(Collectors.groupingBy(s -> s.activityLabel, Collectors. ...
}
}
I don't know how to obtain this result :(. I tried different methods and i couldn't find out how obtain the total duration for each activity... Thank you for helping me!
回答1:
java-8 Duration is different with joda Duration. how about this:
Map<String, Duration> durations = list.stream().collect(Collectors.toMap(
it -> it.activityLabel,
it -> new Duration(it.startTime, it.endTime),
Duration::plus
));
回答2:
First start to use Java 8 DateTime API. There is a built-in Duration
class. Then you can use:
Map<String, Duration> map = list.stream().collect(
Collectors.groupingBy(a -> a.activityLabel,
Collectors.reducing(Duration.ZERO,
a -> Duration.between(a.startTime, a.endTime).abs(), Duration::plus
)
));
EDIT
Since you've to use Joda
you can get the same results with (using Period
instead of Duration
):
Map<String, Period> map = list.stream().collect(
Collectors.groupingBy(a -> a.activityLabel,
Collectors.reducing(Period.ZERO,
a -> new Period(a.startTime, a.endTime), Period::plus
)
));
来源:https://stackoverflow.com/questions/44086903/how-to-add-datetime-values-in-a-streamjava