如何将列表的列表更改为列表

How to change a list of lists into a list

是否有 lambda 表达式,或者内置于 java 中的东西 将列表的列表更改为一个列表。

例如-->

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. ????? 
}

有没有一种奇特的方法可以将所有列表返回到一个列表中? 我无法使用这些关键字在堆栈上找到这个问题。

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());
}

这回答了您关于将 List<List<>> 转换为扁平化 List<> 的原始问题。

甚至

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());
}

这适用于您的原始示例,但可能无法直接回答您的问题。

重写排序:

public List<LocalDateTime> getallsort(){
  return Stream.of(
    getElite(true),
    getRecreation(true),
    getYouth(true)
    )
    .sorted(comparingInt(List::size).reversed())
    .flatMap(List::stream)
    .collect(Collectors.toList());
}