Is there anything in Java close to the parallel collections in Scala?

2 min read Original article ↗

http://gee.cs.oswego.edu/dl/jsr166/dist/extra166ydocs/

Don't give up so fast, snappy! ))

From the javadocs (with changes to map to your f) the essential matter is really just this:

ParallelLongArray a = ... // you provide
a.replaceWithMapping (new LongOp() { public long op(long a){return a*2L;}};);

is pretty much this, right?

val list = aLargeList
list.par.map(_*2)

& If you are willing to live with a bit less terseness, the above can be a reasonably clean and clear 3 liner (and of course, if you reuse functions, then its the same exact thing as Scala - inline functions.):

ParallelLongArray a = ... // you provide
LongOp f = new LongOp() { public long op(long a){return a*2L;}};
a.replaceWithMapping (f);

[edited above to show concise complete form ala OP's Scala variant]

and here it is in maximal verbose form where we start from scratch for demo:

import java.util.Random;
import jsr166y.ForkJoinPool;
import extra166y.Ops.LongGenerator;
import extra166y.Ops.LongOp;
import extra166y.ParallelLongArray;

public class ListParUnaryFunc {
    public static void main(String[] args) {

        int n = Integer.parseInt(args[0]);
        // create a parallel long array 
        // with random long values
        ParallelLongArray a =  ParallelLongArray.create(n-1, new ForkJoinPool());
        a.replaceWithGeneratedValue(generator);

        // use it: apply unaryLongFuncOp in parallel 
        //         to all values in array
        a.replaceWithMapping(unaryLongFuncOp);

        // examine it
        for(Long v : a.asList()){
            System.out.format("%d\n", v);
        }
    }

    static final Random rand = new Random(System.nanoTime());
    static LongGenerator generator = new LongGenerator() {
        @Override final
        public long op() { return rand.nextLong(); }
    };

    static LongOp unaryLongFuncOp = new LongOp() {
        @Override final public long op(long a) { return a * 2L; }
    };
}

Final edit and notes:

Also note that a simple class such as the following (which you can reuse across your projects):

/**
 * The very basic form w/ TODOs on checks, concurrency issues, init, etc.
 */
final public static class ParArray {
    private ParallelLongArray parr;
    private final long[] arr;
    public ParArray (long[] arr){
        this.arr = arr;
    }
    public final ParArray par() {
        if(parr == null)
            parr = ParallelLongArray.createFromCopy(arr, new ForkJoinPool()) ;
        return this;
    }
    public final ParallelLongArray map(LongOp op) {
        return parr.replaceWithMapping(op);
    }
    public final long[] values() { return parr.getArray(); }
}

and something like that will allow you to write more fluid Java code (if terseness matters to you):

long[] arr = ... // you provide
LongOp f = ... // you provide

ParArray list = new ParArray(arr);
list.par().map(f);

And the above approach can certainly be pushed to make it even cleaner.