Is there a lambda expression, or something build into java to change a List of Lists into one List.
for instance -->
public List<LocalDateTime> getallsort(){
List<LocalDateTime> elite = getElite(true);
List<LocalDateTime> recreation = getRecreation(true);
List<LocalDateTime> youth = getYouth(true);
List<List<LocalDateTime>> list = Arrays.asList(elite,recreation,youth);
list.sort((xs1, xs2) -> xs1.size() - xs2.size());
return list. ?????
}
Is there a fancy way of returning all lists into 1 list? I wasn't able to find this question on stack using these keywords.
CodePudding user response:
public List<LocalDateTime> getallsort(){
List<LocalDateTime> elite = getElite(true);
List<LocalDateTime> recreation = getRecreation(true);
List<LocalDateTime> youth = getYouth(true);
List<List<LocalDateTime>> list = Arrays.asList(elite,recreation,youth);
list.sort((xs1, xs2) -> xs1.size() - xs2.size());
return list.stream().flatMap(List::stream).collect(Collectors.toList());
}
which answers your original question about converting a List<List<>>
into a flattened List<>
.
Or even
public List<LocalDateTime> getallsort(){
return Stream.of(
getElite(true),
getRecreation(true),
getYouth(true)
)
.sorted((xs1, xs2) -> xs1.size() - xs2.size())
.flatMap(List::stream)
.collect(Collectors.toList());
}
which works for your original example, but maybe does not directly answer your question.