Java Iterating through a List
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class Collection3 {
public static void main(String[] args) {
Below is the Collectiion using List
List<String> lists = new ArrayList<String>();
lists.add("Cainton Milroy");
lists.add("Jerima Milroy");
lists.add("Kenzilla Milroy");
The habitual way of iterating in Java Loops, but not desirable way of iterating and printing each of the elements
for(int i = 0; i < lists.size();i++){
System.out.println(lists.get(i));
}
The Java good way of iterating through a loops than the traditional way of iterating through a loop
for(String element : lists){
System.out.println("Elements : "+ element);
}
System.out.println("_-_-_-_-_Second For Loop-_-_-_-_-_-_-_-");
Iterator<String> itr = lists.iterator();
while(itr.hasNext()){
String element = itr.next();
System.out.println("Element: " + element);
}
System.out.println("_-_-_-_-Lambda Expression_-_-_-_-_-_-_-_-");
lists.forEach((final String name) -> System.out.println(name));
}
}
Written by Cainton Milroy
Related protips
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Tags
Authors
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#