Path: csiph.com!news.swapon.de!eternal-september.org!feeder.eternal-september.org!mx02.eternal-september.org!.POSTED!not-for-mail From: Eric Sosman Newsgroups: comp.lang.java.programmer Subject: Re: parallel stream with return Date: Wed, 30 Sep 2015 08:01:07 -0400 Organization: A noiseless patient Spider Lines: 24 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=windows-1252; format=flowed Content-Transfer-Encoding: 7bit Injection-Date: Wed, 30 Sep 2015 11:59:10 +0000 (UTC) Injection-Info: mx02.eternal-september.org; posting-host="858a9d65d95f4ea4d355bd116aa2cdbf"; logging-data="1933"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+ZJfcgIi23UZxeQz5IfldH" User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:38.0) Gecko/20100101 Thunderbird/38.2.0 In-Reply-To: Cancel-Lock: sha1:Roz7DP7xRMM1pqLu4ZYj7j6T4IM= Xref: csiph.com comp.lang.java.programmer:32736 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 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(). -- esosman@comcast-dot-net.invalid "Don't be afraid of work. Make work afraid of you." -- TLM