forEach与jdk8中的lambda, Stream

增强for循环 :forEach

反编译后可以看到实际使用的仍然是Iterator+while遍历的

forEach的优点是写法简单,缺点是不能使用xxx.remove(e)或者iter.remove(), 如果有删除的需要,还是老老实实用Iterator

  对于数组的遍历,使用的是length遍历

参考:

https://stackoverflow.com/questions/85190/how-does-the-java-for-each-loop-work

https://docs.oracle.com/javase/specs/jls/se8/html/jls-14.html#jls-14.14.2

lambda的forEach,注意不能使用break, continue,  而reture相当于普通遍历中的continue。也就是没有普通遍历中的break和return的功能。 

jdk8中Map也有forEach 了, 它是遍历keyEntry

Stream中的forEach ...

原文地址:https://www.cnblogs.com/yszzu/p/9532936.html