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


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

Re: parallel stream with return

From Jeff Higgins <jeff@invalid.invalid>
Newsgroups comp.lang.java.programmer
Subject Re: parallel stream with return
Date 2015-09-30 16:53 -0400
Organization A noiseless patient Spider
Message-ID <muhi14$7ep$1@dont-email.me> (permalink)
References <e585b3de-181e-4963-8a00-341749bf00ab@googlegroups.com> <muhhb6$4fi$1@dont-email.me>

Show all headers | View raw


On 09/30/2015 04:41 PM, Jeff Higgins wrote:
> On 09/30/2015 02: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?
>>
> What's wrong with what you had to start?
> Have I misunderstood?
>
> import java.util.HashSet;
> import java.util.stream.IntStream;
>
> public class Scratch {
>
>    private final int count;
>    private final String name;
>
>    private Scratch(int i, String t) { count = i; name = t;}
>
>    public static void main(String[] args) {
>      HashSet<Scratch> x = new HashSet<>();
>      IntStream.range(0, 4096).parallel()
>        .forEach(i -> { x.add(generate(i)); });
>      for (Scratch s : x) {
>        System.out.println(s.count + " : " + s.name);
>      }
>    }
>
>    private static Scratch generate(int i) {
>      return new Scratch(i, Thread.currentThread().getName());
>    }
>
> }
>
>
>

Or:

import java.util.ArrayList;
import java.util.List;
import java.util.stream.IntStream;

public class Scratch {

   private final int count;
   private final String name;

   private Scratch(int i, String t) { count = i; name = t;}

   public static void main(String[] args) {
     List<Scratch> x = new ArrayList<>();
     IntStream.range(0, 4096).parallel()
       .forEachOrdered(i -> { x.add(generate(i)); });
     for (Scratch s : x) {
       System.out.println(s.count + " : " + s.name);
     }
   }

   private static Scratch generate(int i) {
     return new Scratch(i, Thread.currentThread().getName());
   }

}

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