I have a List of Rec's and I want to process them all in parallel, by calling some method foo().
foo() may flag some Rec's as erroneous and at the end all the error recs are reported by their index position (e.g. "rec 12 is not valid").
So I want to either pass the index position of each Rec to foo(), or set the index into each Rec before calling foo.
What I attempted to do (shown below) is first, sequentially, set the index to each Rec,
and then in parallel call foo on each. Is there a better way to do this?
List<Rec> recs;
class Rec {
private int recordIndex;
public void setRecordIndex( int index) { recordIndex = index; }
//more memebers and getter to the above...
}
//somewhere in the code
int index = 0;
recs.stream().forEach(rec -> rec.setRecordIndex(index++));
//the compiler complains about the index++ above, says it should be final
rec.parallelStream().forEach(rec -> foo(ProgressInfo, rec));
Is there a better way to do this overall? and if not, is there a way to fix the compilation error and still use stream? (instead of looping)