FalAn
FalAn

Reputation: 55

Java group by a list of object contain 2 LocalDateTime field, then get the maximum and also minimum value out of it

i got a class call TimeInterval with 2 fields which are "startDate" and "endDate", and i got a list of data like this:

List here, there still more, but i think these are enough

What i want to do is, take the first 6 result for example, which got start date as "2015-01-19":

Example List here

Now i want to group them by startDate, and take the minimum "hour" value of startDate which is "12", and also take the maximum "hour" value of endDate which is "4".

How do i achieve this? i try to groupBy with stream, but i kinda meet a deadend and have no idea how to solve this.

TimeInterval class:

public class TimeInterval {
private LocalDateTime startDate;
private LocalDateTime endDate;

public LocalDateTime getStartDate() {
    return startDate;
}
public void setStartDate(LocalDateTime startDate) {
    this.startDate = startDate;
}
public LocalDateTime getEndDate() {
    return endDate;
}
public void setEndDate(LocalDateTime endDate) {
    this.endDate = endDate;
}


public TimeInterval(LocalDateTime startDate, LocalDateTime endDate) {
    this.startDate = startDate;
    this.endDate = endDate;
}

@Override
public String toString() {
    return "TimeInterval [startDate=" + startDate + ", endDate=" + endDate +"]";
}
}

Data:

TimeInterval [startDate=2015-01-19T12:00, endDate=2015-01-19T23:59:59]
TimeInterval [startDate=2015-01-19T13:00, endDate=2015-01-19T17:00]
TimeInterval [startDate=2015-01-19T15:30, endDate=2015-01-19T18:30]
TimeInterval [startDate=2015-01-19T17:00, endDate=2015-01-19T18:30]
TimeInterval [startDate=2015-01-19T18:00, endDate=2015-01-19T19:00]
TimeInterval [startDate=2015-01-19T19:00, endDate=2015-01-20T04:00]
TimeInterval [startDate=2015-01-20T06:00, endDate=2015-01-20T21:00]
TimeInterval [startDate=2015-01-20T07:00, endDate=2015-01-20T17:00]
TimeInterval [startDate=2015-01-20T08:00, endDate=2015-01-20T17:00]
TimeInterval [startDate=2015-01-20T08:15, endDate=2015-01-20T17:15]
TimeInterval [startDate=2015-01-20T09:00, endDate=2015-01-20T19:00]
TimeInterval [startDate=2015-01-20T10:00, endDate=2015-01-20T19:00]
TimeInterval [startDate=2015-01-20T10:15, endDate=2015-01-20T19:15]
TimeInterval [startDate=2015-01-20T11:00, endDate=2015-01-20T20:00]
TimeInterval [startDate=2015-01-20T11:30, endDate=2015-01-20T18:00]
TimeInterval [startDate=2015-01-20T12:00, endDate=2015-01-20T23:59:59]
TimeInterval [startDate=2015-01-20T13:00, endDate=2015-01-20T16:00]
TimeInterval [startDate=2015-01-20T15:00, endDate=2015-01-20T22:00]
TimeInterval [startDate=2015-01-20T15:30, endDate=2015-01-20T18:30]
TimeInterval [startDate=2015-01-20T18:00, endDate=2015-01-20T19:00]
TimeInterval [startDate=2015-01-20T19:00, endDate=2015-01-21T04:00]
TimeInterval [startDate=2015-01-21T00:00, endDate=2015-01-21T23:59:59]
TimeInterval [startDate=2015-01-21T07:00, endDate=2015-01-21T17:00]
TimeInterval [startDate=2015-01-21T07:30, endDate=2015-01-21T17:00]
TimeInterval [startDate=2015-01-21T07:45, endDate=2015-01-21T12:45]
TimeInterval [startDate=2015-01-21T08:00, endDate=2015-01-21T17:00]
TimeInterval [startDate=2015-01-21T08:30, endDate=2015-01-21T17:30]
TimeInterval [startDate=2015-01-21T09:00, endDate=2015-01-21T19:00]
TimeInterval [startDate=2015-01-21T10:00, endDate=2015-01-21T19:00]
TimeInterval [startDate=2015-01-21T11:00, endDate=2015-01-21T20:00]
TimeInterval [startDate=2015-01-21T11:30, endDate=2015-01-21T18:00]
TimeInterval [startDate=2015-01-21T12:00, endDate=2015-01-21T23:59:59]
TimeInterval [startDate=2015-01-21T13:00, endDate=2015-01-21T22:00]
TimeInterval [startDate=2015-01-21T14:00, endDate=2015-01-21T20:30]
TimeInterval [startDate=2015-01-21T15:00, endDate=2015-01-21T21:00]
TimeInterval [startDate=2015-01-21T15:30, endDate=2015-01-21T18:30]
TimeInterval [startDate=2015-01-21T18:00, endDate=2015-01-21T19:00]
TimeInterval [startDate=2015-01-21T19:00, endDate=2015-01-21T19:50]
TimeInterval [startDate=2015-01-22T00:00, endDate=2015-01-22T23:59:59]
TimeInterval [startDate=2015-01-22T01:00, endDate=2015-01-22T04:30]
TimeInterval [startDate=2015-01-22T07:00, endDate=2015-01-22T17:00]
TimeInterval [startDate=2015-01-22T08:00, endDate=2015-01-22T17:00]
TimeInterval [startDate=2015-01-22T09:00, endDate=2015-01-22T19:00]
TimeInterval [startDate=2015-01-22T09:30, endDate=2015-01-22T18:30]
TimeInterval [startDate=2015-01-22T10:00, endDate=2015-01-22T19:00]
TimeInterval [startDate=2015-01-22T11:00, endDate=2015-01-22T20:00]
TimeInterval [startDate=2015-01-22T11:30, endDate=2015-01-22T19:30]
TimeInterval [startDate=2015-01-22T12:00, endDate=2015-01-22T23:59:59]
TimeInterval [startDate=2015-01-22T13:00, endDate=2015-01-22T18:00]
TimeInterval [startDate=2015-01-22T14:00, endDate=2015-01-22T22:00]
TimeInterval [startDate=2015-01-22T15:30, endDate=2015-01-22T18:30]
TimeInterval [startDate=2015-01-22T18:00, endDate=2015-01-22T19:00]
TimeInterval [startDate=2015-01-22T19:00, endDate=2015-01-23T04:00]
TimeInterval [startDate=2015-01-23T00:00, endDate=2015-01-23T23:59:59]
TimeInterval [startDate=2015-01-23T05:00, endDate=2015-01-23T17:00]
TimeInterval [startDate=2015-01-23T06:00, endDate=2015-01-23T15:00]
TimeInterval [startDate=2015-01-23T07:00, endDate=2015-01-23T17:00]
TimeInterval [startDate=2015-01-23T07:45, endDate=2015-01-23T12:45]
TimeInterval [startDate=2015-01-23T08:00, endDate=2015-01-23T17:00]
TimeInterval [startDate=2015-01-23T08:15, endDate=2015-01-23T17:15]
TimeInterval [startDate=2015-01-23T09:00, endDate=2015-01-23T22:00]
TimeInterval [startDate=2015-01-23T09:30, endDate=2015-01-23T17:30]
TimeInterval [startDate=2015-01-23T10:00, endDate=2015-01-23T19:00]
TimeInterval [startDate=2015-01-23T10:30, endDate=2015-01-23T17:00]
TimeInterval [startDate=2015-01-23T11:00, endDate=2015-01-23T20:00]
TimeInterval [startDate=2015-01-23T11:30, endDate=2015-01-23T18:00]
TimeInterval [startDate=2015-01-23T12:00, endDate=2015-01-23T23:59:59]

Upvotes: 1

Views: 175

Answers (2)

Eritrean
Eritrean

Reputation: 16498

Assuming you want a map as an end result, you could do

  • Group by Localdate -> Map<LocalDate, List<TimeInterval>>
  • Stream over the entries of resulting map
  • collect to map using the same key (LocalDate) and map to a new TimeInterval Object having the min startDate from the entry's value and max endDate

Example:

Map<LocalDate, TimeInterval> result = 
        sampleList.stream()
                  .collect(Collectors.groupingBy(t -> t.getStartDate().toLocalDate()))
                  .entrySet()
                  .stream()
                  .collect(Collectors.toMap(Entry::getKey,
                                            e -> new TimeInterval(getMinDate(e.getValue()), getMaxDate(e.getValue()))));

with the two helper methods getMaxDate and getMinDate :

private static LocalDateTime getMaxDate(final List<TimeInterval> list) {
    return list.stream()
               .collect(Collectors.reducing(LocalDateTime.MIN,
                                            TimeInterval::getStartDate,
                                            BinaryOperator.maxBy(LocalDateTime::compareTo)));
}
private static LocalDateTime getMinDate(final List<TimeInterval> list) {
    return list.stream()
               .collect(Collectors.reducing(LocalDateTime.MAX,
                                            TimeInterval::getStartDate,
                                            BinaryOperator.minBy(LocalDateTime::compareTo)));
}

Upvotes: 2

Arjun
Arjun

Reputation: 36

You can groupby them using the startDate using the Java Stream API and then sort the resultant data structure for each group once with startDate and once with endDate to get the min and max element for startDate and endDate.

Upvotes: 0

Related Questions