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


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

Re: parallel stream with return

Path csiph.com!eternal-september.org!feeder.eternal-september.org!mx02.eternal-september.org!.POSTED!not-for-mail
From Eric Sosman <esosman@comcast-dot-net.invalid>
Newsgroups comp.lang.java.programmer
Subject Re: parallel stream with return
Date Wed, 30 Sep 2015 13:22:11 -0400
Organization A noiseless patient Spider
Lines 49
Message-ID <muh5kf$fdk$1@dont-email.me> (permalink)
References <e585b3de-181e-4963-8a00-341749bf00ab@googlegroups.com> <mugiqe$1sd$1@dont-email.me> <2b9115f7-1b42-4ee4-9af9-1c8aa84f94d1@googlegroups.com>
Mime-Version 1.0
Content-Type text/plain; charset=windows-1252; format=flowed
Content-Transfer-Encoding 7bit
Injection-Date Wed, 30 Sep 2015 17:20:15 +0000 (UTC)
Injection-Info mx02.eternal-september.org; posting-host="858a9d65d95f4ea4d355bd116aa2cdbf"; logging-data="15796"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19wOejOoIETij+SkOEok9e7"
User-Agent Mozilla/5.0 (Windows NT 6.1; WOW64; rv:38.0) Gecko/20100101 Thunderbird/38.2.0
In-Reply-To <2b9115f7-1b42-4ee4-9af9-1c8aa84f94d1@googlegroups.com>
Cancel-Lock sha1:yA2IDgjE1jKCfSH7Bh/0UhpbURs=
Xref csiph.com comp.lang.java.programmer:32739

Show key headers only | View raw


On 9/30/2015 11:02 AM, Philipp Kraus wrote:
> Am Mittwoch, 30. September 2015 14:01:37 UTC+2 schrieb Eric Sosman:
>> On 9/30/2015 2:35 AM, Philipp Kraus wrote:
>>> Hello,
>>>
>>> I'm new with the Java 1.8 parallel stream, so I have got a problem:
>>>
>>> I have the sequential for loop:
>>>
>>> final Set<myClass> x = new HashSet<>();
>>> for(int i=0; i < value; ++i)
>>>       x.add( this.generate() );
>>>
>>> And I would like to do this with a parallel stream which collects each generate return value into the set.
>>> My first try was to use a synchronized HashSet and
>>>
>>> IntStream.range(0, value).parallel().forEach( i-> { x.add(this.generate()) } );
>>>
>>> After that I try to swap forEach to map, but at this I must return an int value and not a myClass value, so how can I do n generate calls in parallel and collect the results into a hashset?
>>
>>       I think you want java.util.stream.Collectors, either toSet()
>> or toCollection().
>
> but how can I iterate within the range [0,n) in parallel? Can you show me the code of the sequential for loop?

     Untested:

	Set<myClass> x =
	    IntStream.range(0, value)
	    .parallel()
	    .mapToObj(i -> this.generate())
	    .collect(Collectors.toSet());

Note that this won't necessarily produce a HashSet, but is more
likely to produce some other kind of Set.  If you really need an
actual HashSet, you could

     - Use the toCollection() method of Collectors (but I'm not
       100% sure about what kind of synchronization you'll need), or

     - Copy the unknown Set's content into a HashSet after the
       pipeline finishes:

	Set<myClass> x = new HashSet<>(
	    IntStream.range ...

-- 
esosman@comcast-dot-net.invalid
"Don't be afraid of work. Make work afraid of you." -- TLM

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


Thread

parallel stream with return Philipp Kraus <philipp.kraus.flashpixx@gmail.com> - 2015-09-29 23:35 -0700
  Re: parallel stream with return Eric Sosman <esosman@comcast-dot-net.invalid> - 2015-09-30 08:01 -0400
    Re: parallel stream with return Philipp Kraus <philipp.kraus.flashpixx@gmail.com> - 2015-09-30 08:02 -0700
      Re: parallel stream with return Eric Sosman <esosman@comcast-dot-net.invalid> - 2015-09-30 13:22 -0400
        Re: parallel stream with return Steven Simpson <ss@my.domain.invalid> - 2015-09-30 22:25 +0100
  Re: parallel stream with return Jeff Higgins <jeff@invalid.invalid> - 2015-09-30 16:41 -0400
    Re: parallel stream with return Jeff Higgins <jeff@invalid.invalid> - 2015-09-30 16:53 -0400
      Re: parallel stream with return Philipp Kraus <philipp.kraus.flashpixx@gmail.com> - 2015-09-30 23:36 -0700
        Re: parallel stream with return Eric Sosman <esosman@comcast-dot-net.invalid> - 2015-10-01 08:22 -0400
        Re: parallel stream with return Jeff Higgins <jeff@invalid.invalid> - 2015-10-01 08:54 -0400

csiph-web