Consider the following scenario.
List<String> list = new ArrayList<>();
Now I added the String
values for this list.
I used following ways to go each and every element in the list.
Option one- use for-each
for (String i : list) {
System.out.println(i);
}
Option two- use Iterator
Iterator it=list.iterator();
while (it.hasNext()){
System.out.println(it.next());
}
I just want to know is there any performance advantage if I use for-each
instead of Iterator
. And also is it a bad practice to use Iterator now a days in Java?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…