As a result i want to create a new List of Map (see expected result)
<code>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())
);
</code>
<code>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())
);
</code>
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:
<code>[
{
"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"
}
]
</code>
<code>[
{
"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"
}
]
</code>
[
{
"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
This is my implementation
<code>public class Main {
public static Map<String, String> getFirstAndLastDate(Map<String, List<String>> data){
Map<String, String> result = new HashMap<>();
data.forEach((k, v) -> {
result.put("dayOfWeek", k);
if(v.size() == 1){
result.put("startDate", v.get(0));
result.put("endDate", v.get(0));
}else{
result.put("startDate", v.get(0));
result.put("endDate", v.get(v.size()-1));
}
});
return result;
}
public static boolean isEmpty(Map<String, List<String>> data){
Collection<List<String>> values = data.values();
for (List<String> value : values) {
if (value.isEmpty()) {
return false;
}
}
return true;
}
public static void main(String[] args) {
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())
);
List<Map<String, String>> result = dataSource.stream()
.filter(Main::isEmpty)
.map(Main::getFirstAndLastDate)
.toList();
}
}
</code>
<code>public class Main {
public static Map<String, String> getFirstAndLastDate(Map<String, List<String>> data){
Map<String, String> result = new HashMap<>();
data.forEach((k, v) -> {
result.put("dayOfWeek", k);
if(v.size() == 1){
result.put("startDate", v.get(0));
result.put("endDate", v.get(0));
}else{
result.put("startDate", v.get(0));
result.put("endDate", v.get(v.size()-1));
}
});
return result;
}
public static boolean isEmpty(Map<String, List<String>> data){
Collection<List<String>> values = data.values();
for (List<String> value : values) {
if (value.isEmpty()) {
return false;
}
}
return true;
}
public static void main(String[] args) {
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())
);
List<Map<String, String>> result = dataSource.stream()
.filter(Main::isEmpty)
.map(Main::getFirstAndLastDate)
.toList();
}
}
</code>
public class Main {
public static Map<String, String> getFirstAndLastDate(Map<String, List<String>> data){
Map<String, String> result = new HashMap<>();
data.forEach((k, v) -> {
result.put("dayOfWeek", k);
if(v.size() == 1){
result.put("startDate", v.get(0));
result.put("endDate", v.get(0));
}else{
result.put("startDate", v.get(0));
result.put("endDate", v.get(v.size()-1));
}
});
return result;
}
public static boolean isEmpty(Map<String, List<String>> data){
Collection<List<String>> values = data.values();
for (List<String> value : values) {
if (value.isEmpty()) {
return false;
}
}
return true;
}
public static void main(String[] args) {
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())
);
List<Map<String, String>> result = dataSource.stream()
.filter(Main::isEmpty)
.map(Main::getFirstAndLastDate)
.toList();
}
}
I have no big experience of Stream API and i’m not satisfied of this implementation (method getFirstAndLastDate and isEmpty ). Is it possible to improve this code and make it more functional