Using the non final loop variable inside a lambda expression

ぐ巨炮叔叔 提交于 2019-12-18 19:08:38

问题


I'm trying to execute 100 tasks all in parallel via executors and runnable, the task needs to use the loop variable:

for (int i = 0; i < 100; i++) {
   executor.execute(() -> {
     doSomething(String.format("Need task number %d done", i));
   }
  });
}

I get a squiggly under 'i' saying - Variable used in lambda expression should be effectively final.

A loop variable, as far as I'm aware, cannot be made final or effectively final, since it is being changed with each iteration. I found a simple workaround,

for (int i = 0; i < 100; i++) {
   int index = i;
   executor.execute(() -> {
     doSomething(String.format("Need task number %d done", index));
   }
 });
}

This doesn't seem the most effective solution to me, declaring a new variable at every iteration. Is there a better way of doing this?


回答1:


Is there a better way of doing this?

I doubt it. Your solution looks fine to me, but if you want you can rewrite it into possibly clearer code like:

IntStream.range(0, 100).forEach(
    i -> executor.execute(
        () -> doSomething(String.format("Need task number %d done", i))
    )
);



回答2:


That's the most straightforward way to make it work. You're not going to do better than that.



来源:https://stackoverflow.com/questions/32533621/using-the-non-final-loop-variable-inside-a-lambda-expression

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!