i have these five way to iterate through ArrayList and these are
public static void main(String[] argv) {
// create list
List<String> aList = new ArrayList<String>();
// add 4 different values to list
aList.add("eBay");
aList.add("Paypal");
aList.add("Google");
aList.add("Yahoo");
// iterate via "for loop"
System.out.println("==> For Loop Example.");
for (int i = 0; i < aList.size(); i++) {
System.out.println(aList.get(i));
}
// iterate via "New way to loop"
System.out.println("\n==> Advance For Loop Example..");
for (String temp : aList) {
System.out.println(temp);
}
// iterate via "iterator loop"
System.out.println("\n==> Iterator Example...");
Iterator<String> aIterator = aList.iterator();
while (aIterator.hasNext()) {
System.out.println(aIterator.next());
}
// iterate via "while loop"
System.out.println("\n==> While Loop Example....");
int i = 0;
while (i < aList.size()) {
System.out.println(aList.get(i));
i++;
}
// collection stream() util: Returns a sequential Stream with this collection as its source
System.out.println("\n==> collection stream() util....");
aList.forEach((temp) -> {
System.out.println(temp);
});
}
My question is iterating thru any of these way is same or there is any difference? if they, then which is the best way of doing this, my requirement is removing a element from an arraylist based on some condition.