I need to collect an entity records as from date to date, based on one attribute's value. If the value of type attribute is sequentially same based on date, it should be grouped by date. Since date mentioned as sequentially, should be ordered. Even if the value of type attribute of a record is different, rest of the records also should be under the same day. See the visual representation. I've tried this;
Map<LocalDate, List<Entity>> collection = entities.stream().collect(Collectors.groupingBy(Entity::getDate))
.entrySet().stream().sorted(Map.Entry.comparingByKey()).collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (oldValue, newValue) -> oldValue, LinkedHashMap::new));
In my implementation, I am able to only collect by dates, but I want to collect by DateRange. I want to achieve some thing like this;
Map<DateRange, List<Entity>> collection = entities.stream()...// implementation
Entity
[
{
"id": 1,
"date": "2020-01-01",
"type": 5
},
{
"id": 2,
"date": "2020-01-01",
"type": 5
},
{
"id": 1,
"date": "2020-01-02",
"type": 5
},
{
"id": 2,
"date": "2020-01-02",
"type": 5
},
.
.
.
]
Example
Date range changes based on the value of the type attribute. For example, if type=5 for all dates, so all records should be in one range. Let's say there are only records for one year and I'm assuming there are only two unique id value(id=1, id=2), so in collection I should achieve this;
[{
"From: 2020-01-01, To: 2020-12-31": [{
"record1":
{
"id": 1,
"type": "5"
},
"record2":
{
"id": 2,
"type": "5"
}
}]
}]
Another example
If the type=5 for all dates except '2020-02-01' and in '2020-02-01' type=6 for the id=1, then ranges should be like the below. I'm still assuming, there are records only for one year and there are only two unique id value(id=1, id=2).
[
{
"From: 2020-01-01, To: 2020-01-31": [{
"record1":
{
"id": 1,
"type": "5"
},
"record2":
{
"id": 2,
"type": "5"
}
}],
},
{
"From: 2020-02-01, To: 2020-02-01": [{
"record1":
{
"id": 1,
"type": "6"
},
"record2":
{
"id": 2,
"type": "5"
}
}],
},
{
"From: 2020-02-02, To: 2020-12-31": [{
"record1":
{
"id": 1,
"type": "5"
},
"record2":
{
"id": 2,
"type": "5"
}
}]
}
]