[英]How can I use merge method in Java to append elements to the list value in map<Character,List<Integer>>?
[英]JAVA How to append the return value of .map() in a list instead of returning a list of list
我有一個 java 代碼類似於
/*Statement A*/
dataList.stream.map(ClassA::mapFunction).collect(Collectors.toList())
關鍵是 mapFunction 具有以下定義:
List<Entry> mapFunction(DataListItem dataListItem)
因此上面的 stream().map() 調用返回List<List<Entry>>
我想要的是返回值應該是list<Entry>
(基本上附加所有返回值),為此我目前正在做的是:
final List<Entry> entryList = new ArrayList<>();
dataList.stream.map(dataListItem -> entryList.addAll(ClassA.mapFunction(dataListItem))
.collect(Collectors.toList());
有沒有一種干凈的方法來 append mapFunction 返回值,使得語句 A 的結果只是List<Entry>
而不是List<List<Entry>>
。
flatMap
操作,這是您對與map
、 filter
、 collect
等相同的流執行的操作,它執行以下操作:
You supply a lambda that turns each element in the stream into a stream itself (so a stream of streams), and then the 'output' of the operation is a single stream consisting of all these streams concatenated together.
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.