使用Lambdas将列表列表转换为列表 [英] Turn a List of Lists into a List Using Lambdas

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

问题描述

如果我有一个 List< List< Object>> ,我可以把它变成 List< Object>

If I have a List<List<Object>> can I turn it into a List<Object> that contains all the objects using continuous (not breaking the invocation sequence) invocations of lambdas?

推荐答案

可以使用 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());

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

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