Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Grouping items by date

I have items in my list and the items have a field, which shows the creation date of the item.

enter image description here

and I need to group them based on a "compression", which the user gives. The options are Day, Week, Month and Year.

If the user selects day compression, I need to group my items as such that the items, which are created in the same day, will be groupped. In my example above, only item 1 and item 2 are created in the same day. The others are also groups but they will have only one item because at their day, only one item is created.

{{item1, item2}, {item3}, {item4}, {item5}, {item6}, {item7}}

If the user selects week:

{{item1, item2, item3, item4}, {item5}, {item6}, {item7}}

If the user selects month:

{{item1, item2, item3, item4, item5}, {item6}, {item7}}

If the user selects year:

{{item1, item2, item3, item4, item5, item6}, {item7}}

After groups are created, the date of the items are not important. I mean the key can be anything, as long as the groups are created.

In case of usage of Map, I thought as the keys as follow:

day = day of the year
week = week of the year
month = month of the year
year = year

What would be the best solution to this problem? I could not even start it an I cannot think of a solution other than iteration.

like image 995
drJava Avatar asked Jul 12 '17 14:07

drJava


2 Answers

I would use Collectors.groupingBy with an adjusted LocalDate on the classifier, so that items with similar dates (according to the compression given by the user) are grouped together.

For this, first create the following Map:

static final Map<String, TemporalAdjuster> ADJUSTERS = new HashMap<>();

ADJUSTERS.put("day", TemporalAdjusters.ofDateAdjuster(d -> d)); // identity
ADJUSTERS.put("week", TemporalAdjusters.previousOrSame(DayOfWeek.of(1)));
ADJUSTERS.put("month", TemporalAdjusters.firstDayOfMonth());
ADJUSTERS.put("year", TemporalAdjusters.firstDayOfYear());

Note: for "day", a TemporalAdjuster that lets the date untouched is being used.

Next, use the compression given by the user to dynamically select how to group your list of items:

Map<LocalDate, List<Item>> result = list.stream()
    .collect(Collectors.groupingBy(item -> item.getCreationDate()
            .with(ADJUSTERS.get(compression))));

The LocalDate is adjusted by means of the LocalDate.with(TemporalAdjuster) method.

like image 189
fps Avatar answered Sep 21 '22 23:09

fps


You can get the behaviour you describe with java 8 streams:

Map<LocalDate, List<Data>> byYear = data.stream()
        .collect(groupingBy(d -> d.getDate().withMonth(1).withDayOfMonth(1)));
Map<LocalDate, List<Data>> byMonth = data.stream()
        .collect(groupingBy(d -> d.getDate().withDayOfMonth(1)));
Map<LocalDate, List<Data>> byWeek = data.stream()
        .collect(groupingBy(d -> d.getDate().with(TemporalAdjusters.previousOrSame(DayOfWeek.MONDAY))));
Map<LocalDate, List<Data>> byDay = data.stream()
        .collect(groupingBy(d -> d.getDate()));

Docs for groupingBy and collect. In all 4 cases LocalDate is used as key. To group appropriately, it is modified so that all dates have the same month and day or same day but different month or same month and same day of week (Monday) which leads to obvious grouping rules. The date in your data is not modified only the key. This will consider that the month is the same when also the year is the same and the day is the same when the full date is the same.

For example when grouping by month these dates will have the same key:

01/01/2017 --> key 01/01/2017
04/01/2017 --> key 01/01/2017
05/01/2017 --> key 01/01/2017

and when grouping by week these dates will have the same key (date is previous monday):

04/01/2017 --> key 02/01/2017
05/01/2017 --> key 02/01/2017

You may want instead to group by same day of month for example regardless of year and month. You would achieve it like this:

Map<Integer, List<Data>> byDayOfMonth = data.stream()
        .collect(groupingBy(d -> d.getDate().getDayOfMonth()));

Which would group together 01/01/2017 with 01/10/2017 and then 05/01/2017 with 05/04/2018

like image 43
Manos Nikolaidis Avatar answered Sep 19 '22 23:09

Manos Nikolaidis