如何在Java 8中将列表列表转换为列表?

本文翻译自:How can I turn a List of Lists into a List in Java 8?

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


#1楼

参考:https://stackoom.com/question/1hVuI/如何在Java-中将列表列表转换为列表


#2楼

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: 您可以使用flatMap将内部列表(将它们转换为Streams之后)展平为单个Stream,然后将结果收集到列表中:

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

#3楼

You can use the flatCollect() pattern from Eclipse Collections . 您可以使用Eclipse Collections中flatCollect()模式。

MutableList<List<Object>> list = Lists.mutable.empty();
MutableList<Object> flat = list.flatCollect(each -> each);

If you can't change list from List : 如果您无法从List更改列表:

List<List<Object>> list = new ArrayList<>();
List<Object> flat = ListAdapter.adapt(list).flatCollect(each -> each);

Note: I am a contributor to Eclipse Collections. 注意:我是Eclipse Collections的撰稿人。


#4楼

flatmap is better but there are other ways to achieve the same flatmap更好,但还有其他方法可以实现相同的效果

List<List<Object>> listOfList = ... // fill

List<Object> collect = 
      listOfList.stream()
                .collect(ArrayList::new, List::addAll, List::addAll);

#5楼

I just want to explain one more scenario like List<Documents> , this list contains a few more lists of other documents like List<Excel> , List<Word> , List<PowerPoint> . 我只想解释一个类似List<Documents>场景,该列表包含其他一些列表的其他列表,例如List<Excel>List<Word>List<PowerPoint> So the structure is 所以结构是

class A {
  List<Documents> documentList;
}

class Documents {
  List<Excel> excels;
  List<Word> words;
  List<PowerPoint> ppt;
}

Now if you want to iterate Excel only from documents then do something like below.. 现在,如果您只想从文档中迭代Excel,请执行以下操作。

So the code would be 所以代码是

 List<Documents> documentList = new A().getDocumentList();

 //check documentList as not null

 Optional<Excel> excelOptional = documentList.stream()
                         .map(doc -> doc.getExcel())
                         .flatMap(List::stream).findFirst();
 if(excelOptional.isPresent()){
   Excel exl = optionalExcel.get();
   // now get the value what you want.
 }

I hope this can solve someone's issue while coding... 我希望这可以解决编码时某人的问题...


#6楼

The flatMap method on Stream can certainly flatten those lists for you, but it must create Stream objects for element, then a Stream for the result. flatMap的方法Stream肯定可以拼合那些列出了你,但它必须创建Stream的元素对象,然后Stream的结果。

You don't need all those Stream objects. 您不需要所有这些Stream对象。 Here is the simple, concise code to perform the task. 这是执行任务的简单代码。

// listOfLists is a List<List<Object>>.
List<Object> result = new ArrayList<>();
listOfLists.forEach(result::addAll);

Because a List is Iterable , this code calls the forEach method (Java 8 feature), which is inherited from Iterable . 因为ListIterable ,所以这段代码调用Iterable继承forEach方法 (Java 8功能)。

Performs the given action for each element of the Iterable until all elements have been processed or the action throws an exception. Iterable每个元素执行给定的操作,直到已处理完所有元素或该操作引发异常。 Actions are performed in the order of iteration, if that order is specified. 如果指定了顺序,则按照迭代顺序执行操作。

And a List 's Iterator returns items in sequential order. ListIterator按顺序返回项目。

For the Consumer , this code passes in a method reference (Java 8 feature) to the pre-Java 8 method List.addAll to add the inner list elements sequentially. 对于Consumer ,此代码将方法引用(Java 8功能)传递给Java 8之前的方法List.addAll以顺序添加内部列表元素。

Appends all of the elements in the specified collection to the end of this list, in the order that they are returned by the specified collection's iterator (optional operation). 按照指定集合的​​迭代器返回的顺序,将指定集合中的所有元素追加到此列表的末尾(可选操作)。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值