break added to avoid unnecessary loop iterations. Get rid of for since a loop is not needed for removeIf method
@@ -27,13 +27,12 @@ public class RemoveFromList {
while (iterator.hasNext()) {
if (iterator.next().equals("Boxing")) {
iterator.remove();
+ break;
}
}
// ArrayList removeIf method (Java 8)
- for (int i = 0; i < sports.size(); i++) {
sports.removeIf(p -> p.equals("Cycling"));
- }
System.out.println("List after removing: " + sports);
}
