Let say I have an array with a thousands independent objects inside. Now I want to pass over each of them and perform the same operation, for instance, change the value of a specific field.
At first glance, there are multiple approaches in Java 8 to such task, for instance:
forEach();
with lambda expression inside:
itemsArr.forEach(item -> item.setValue("test"));
forEach();
with iterator.- Separate array to a number batches/blocks and deal each batch in a separate thread. For instance: define 2 threads, elements from #0 to 999 will be executed in a thread «A» and the rest in a thread «B».
The final result should be: 100% of array elements should have cared.
What is the optimal approach to such task?
Update:
There is a similar question but talks about another aspect, I'm interesting not in different types of loops performance comparison (while
, for
, forEach
) but in a performance comparison of threats vs. lambdas in a task of array traversal?