如何在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). 按照指定集合的​​迭代器返回的順序,將指定集合中的所有元素追加到此列表的末尾(可選操作)。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章