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


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

Re: parallel stream with return

From Steven Simpson <ss@my.domain.invalid>
Newsgroups comp.lang.java.programmer
Subject Re: parallel stream with return
Date 2015-09-30 22:25 +0100
Organization A noiseless patient Spider
Message-ID <3k0udc-4m6.ln1@s.simpson148.btinternet.com> (permalink)
References <e585b3de-181e-4963-8a00-341749bf00ab@googlegroups.com> <mugiqe$1sd$1@dont-email.me> <2b9115f7-1b42-4ee4-9af9-1c8aa84f94d1@googlegroups.com> <muh5kf$fdk$1@dont-email.me>

Show all headers | View raw


On 30/09/15 18:22, Eric Sosman wrote:
> 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

import java.util.*;
import java.util.function.*;
import java.util.stream.*;

public class Slop {
     private static Object generate() {
         return new Object();
     }

     public static void main(String[] args) throws Exception {
         Collection<Object> foo =
             IntStream.range(0, 4096)
             .parallel()
             .mapToObj(i -> generate())
             .collect(Collectors.toCollection(() -> new HashSet<Object>()));
         System.out.println(foo);
     }
}



> (but I'm not
>       100% sure about what kind of synchronization you'll need), or

<http://docs.oracle.com/javase/8/docs/api/java/util/stream/Stream.html#collect-java.util.stream.Collector->
> When executed in parallel, multiple intermediate results may be 
> instantiated, populated, and merged so as to maintain isolation of 
> mutable data structures. Therefore, even when executed in parallel 
> with non-thread-safe data structures (such as |ArrayList|), no 
> additional synchronization is needed for a parallel reduction.

Perhaps it would be interesting to see whether the lambda provided to 
toCollection is invoked multiple times...

import java.util.*;
import java.util.concurrent.atomic.*;
import java.util.function.*;
import java.util.stream.*;

public class Slop {
     private static Object generate() {
         return new Object();
     }

     private static AtomicInteger counter = new AtomicInteger();

     private static class MySet extends HashSet<Object> {
         public MySet() {
             counter.getAndIncrement();
         }
     }

     public static void main(String[] args) throws Exception {
         Collection<Object> foo =
             IntStream.range(0, 4096)
             .parallel()
             .mapToObj(i -> generate())
             .collect(Collectors.toCollection(() -> new MySet()));
         System.out.println(foo);
         System.out.println(counter);
     }
}

I got '16'.


-- 
ss at comp dot lancs dot ac dot uk

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