本文翻译自: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
. 因为List
是Iterable
,所以这段代码调用了从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. List
的Iterator
按顺序返回项目。
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). 按照指定集合的迭代器返回的顺序,将指定集合中的所有元素追加到此列表的末尾(可选操作)。