Iterate through Collection in Java

Problem

Given the following Collection:

Collection<Integer> collection = Arrays.asList(1, 2, 3);

How to iterate through the items of this Collection?

Solution

Java 8 and above

collection.forEach(i -> {
    System.out.println(i);
});

Java 6 and above

for (Integer i : collection) {
    System.out.println(i);
}