People who have played enough with the brand new Java 8 will be familiar with the new forEach(fn(x)) loop in lambda. This syntax is quite popular among developers because it’s easier and prettier to use than the normal For-Each loop. It is also popular in other languages, especially JavaScript.

The problem is, I’ve seen a very specific bug, which is related to this “functional” forEach() loop, quite a few times, again and again. People tend to think that these two kinds of loop are 100% identical, which is not. Although Java 8’s type-safety make it harder for people to make this mistake because of its compilation error, it’s still not 100% safe.