As a result i want to create a new List of Map (see expected result)
List<Map<String, List<String>>> dataSource = List.of(
Map.of("Sunday",List.of()),
Map.of("Monday", List.of("2024-06-11 01:00:00", "2024-06-11 02:00:00", "2024-06-11 03:00:00", "2024-06-11 04:00:00")),
Map.of("Tuesday", List.of("2024-06-11 04:00:00", "2024-06-11 05:00:00", "2024-06-11 06:00:00")),
Map.of("Wednesday", List.of("2024-06-11 07:00:00", "2024-06-11 08:00:00", "2024-06-11 09:00:00")),
Map.of("Thursday",List.of("2024-06-11 10:00:00")),
Map.of("Friday",List.of())
);
Expected result is:
[
{
"dayOfWeek":"Monday",
"startDate":"2024-06-11 01:00:00",
"endDate":"2024-06-11 04:00:00"
},
{
"dayOfWeek":"Tuesday",
"startDate":"2024-06-11 04:00:00",
"endDate":"2024-06-11 06:00:00"
},
{
"dayOfWeek":"Wednesday",
"startDate":"2024-06-11 07:00:00",
"endDate":"2024-06-11 09:00:00"
},
{
"dayOfWeek":"Thursday",
"startDate":"2024-06-11 10:00:00",
"endDate":"2024-06-11 10:00:00"
}
]
Condition is :
- If empty list just skip it see (“Sunday” and “Friday”)
- If one item in list set is as “startDate” and “endDate” as you can see for “Thursday”
- If more than one element on list just get first item and set it as “startDate” and last item just set it as “endDate” (“Monday”,”Tuesday”, “Wednesday” )
It would be a nice to implement this using Stream API.
The key on the dataSource can be different so do not use hard coded key of map
1