如何在Java 8中将列表列表转换为列表? [英] How can I turn a List of Lists into a List in Java 8?

查看:114
本文介绍了如何在Java 8中将列表列表转换为列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有列表< List< Object>> ,如何将其转换为列表< Object> 包含使用Java 8功能的相同迭代顺序中的所有对象?

If I have a List<List<Object>>, how can I turn that into a List<Object> that contains all the objects in the same iteration order by using the features of Java 8?

推荐答案

您可以使用< a href =https://docs.oracle.com/javase/8/docs/api/java/util/stream/Stream.html#flatMap-java.util.function.Function- =noreferrer> flatMap 将内部列表(将它们转换为Streams后)展平为单个Stream,然后将结果收集到列表中:

You can use flatMap to flatten the internal lists (after converting them to Streams) into a single Stream, and then collect the result into a list:

List<List<Object>> list = ...
List<Object> flat = 
    list.stream()
        .flatMap(List::stream)
        .collect(Collectors.toList());

这篇关于如何在Java 8中将列表列表转换为列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆