我有一个Rec's的列表,我想通过调用一些方法来并行处理它们foo()。
foo()可能会将 some 标记Rec为错误,最后所有错误记录都通过它们的索引位置报告(例如“rec 12 is not valid”)。
所以我想要么将每个 Rec 的索引位置传递给 foo(),要么在调用 foo 之前将索引设置到每个 Rec 中。
我试图做的(如下所示)是首先,按顺序将索引设置为每个 Rec,然后在每个 Rec 上并行调用 foo。有一个更好的方法吗?
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));
有没有更好的方法来做到这一点?如果没有,有没有办法修复编译错误并仍然使用流?(而不是循环)
慕容3067478
千巷猫影
相关分类