Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #33360

parallel stream ordering

Newsgroups comp.lang.java.programmer
Date 2015-12-16 23:47 -0800
Message-ID <36e2751f-2deb-49da-8b67-d16ba5196d33@googlegroups.com> (permalink)
Subject parallel stream ordering
From Philipp Kraus <philipp.kraus.flashpixx@gmail.com>

Show all headers | View raw


Hello,

I'm using calls like

m_annotationexecution.parallelStream().flatMap( i -> i.execute( p_context, null ).stream() ).collect( Collectors.toList() )

execute returns a List, so the flatMap enrolls the list elements to collect them into a new list, but I would like to fix the order. For example m_annotation is a list like

[ "a", "b", "c" ]

execute transform it to

a -> ["a", "1", ...]
b -> ["b", "2",  ...]
c -> ["c", "3", ....]

the flatMap enrolls it to

["a", "1", ..., "b", "2", ..., "c", "3", ....]

but on a parallel stream the order is not fixed, so I can get also

["b", "2", ..., "a", "1", ..., "c", "3", ....] or ["c", "3", ..., "a", "1", ..., "b", "2", ....]

So how can I fix the order, so that the first element in m_annotation is also the first element within the result list? execute and flatMap can be run in parallel, but only the collect must create the correct ordering. 
If Iuse this input [ "a", "b", "c" ] the output should be ["a", "1", ..., "b", "2", ..., "c", "3", ....] during execution the temporal order can be changed

Thanks

Phil

Back to comp.lang.java.programmer | Previous | Next — Next in thread | Find similar | Unroll thread


Thread

parallel stream ordering Philipp Kraus <philipp.kraus.flashpixx@gmail.com> - 2015-12-16 23:47 -0800
  Re: parallel stream ordering Lew <lewbloch@gmail.com> - 2015-12-17 13:38 -0800
    Re: parallel stream ordering Sebastian <news@seyweiler.dyndns.org> - 2015-12-18 23:11 +0100

csiph-web