Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I parallelize the mapping of a list?

This is the problem I have: I have a large sequence of some objects (List<SomeClass>), and want to perform some operation on all elements of list and get a new sequence (List<SomeOtherClass>).

Like:

List<SomeOtherClass> list = new ArrayList<SomeOtherClass>();
for(SomeClass sc : originalList) 
  list.add(someOperation(sc));

Since operation someOperation does not have any side effects, and the list is quite large, I want this mapping operation to be parallelized.

What will be the best way to do that in Java?

like image 981
Grescent Mercury Avatar asked Jun 04 '11 17:06

Grescent Mercury


3 Answers

A possible implementation can utilize the Executor framework (example included).

like image 127
Howard Avatar answered Nov 15 '22 21:11

Howard


Use threading and partition your work using sublists.

like image 1
Marcelo Avatar answered Nov 15 '22 22:11

Marcelo


Split the input list, and use FutureTask task, then merge the results

like image 1
Op De Cirkel Avatar answered Nov 15 '22 22:11

Op De Cirkel