Philipp :
I try to get my head around java streams.
Atm I have this construct which does not work:
List<int[]> whiteLists = processes.stream()
.map(Process::getEventList)
.forEach(eventList -> eventList.stream()
.map(event -> event.getPropertie("whitelist"))
.map(propertie -> propertie.getIntArray())
.collect(Collectors.toList()));
}
The hierarchy is:
- Process
- Event
- Property
- Event
Process::getEventList returns a list of Event
objects
event.getPropertie("whitelist")
returns a Property
objects which hast the method getIntArray()
event.getPropertie()
gives me an int-array.
How do I collect these array into a List of arrays?
Thanks!
Kayaman :
You can't use forEach()
as it takes a Consumer
, meaning it will consume the stream, but can't return anything (so nothing to collect).
You need flatMap to stream the internal eventList
as follows
List<int[]> whiteLists = processes.stream()
.flatMap(p -> p.getEventList().stream())
.map(event -> event.getPropertie("whitelist"))
.map(propertie -> propertie.getIntArray())
.collect(Collectors.toList());
Guess you like
Origin http://43.154.161.224:23101/article/api/json?id=340896&siteId=1