For-each vs Iterator. Which will be the better option

后端 未结 8 1329
悲&欢浪女
悲&欢浪女 2020-12-02 09:14

Consider the following scenario.

List list = new ArrayList<>();

Now I added the String values for this li

8条回答
  •  庸人自扰
    2020-12-02 09:47

    Best way to do this is in java 8 is,

    list.forEach(System.out::println);
    

    Here are some useful links.

    1. Java 8 Iterable.forEach() vs foreach loop

    2. http://www.javaworld.com/article/2461744/java-language/java-language-iterating-over-collections-in-java-8.html

    3. https://docs.oracle.com/javase/8/docs/api/java/lang/Iterable.html

提交回复
热议问题