-1

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.

YoYo
  • 9,157
  • 8
  • 57
  • 74
Greg W.F.R
  • 544
  • 1
  • 5
  • 13

1 Answers1

2
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.

Rewriting the sort:

public List<LocalDateTime> getallsort(){
  return Stream.of(
    getElite(true),
    getRecreation(true),
    getYouth(true)
    )
    .sorted(comparingInt(List::size).reversed())
    .flatMap(List::stream)
    .collect(Collectors.toList());
}
YoYo
  • 9,157
  • 8
  • 57
  • 74
  • This answer has no value anymore because of other original questions this is the duplicate from. - Left answer for the use of `Stream.of`. – YoYo Dec 06 '21 at 05:46