Inspectopedia Help

Loop can be replaced with 'Collection.removeIf()'

Reports loops which can be collapsed into a single Collection.removeIf call.

Example:

for (Iterator<String> it = collection.iterator(); it.hasNext(); ) { String aValue = it.next(); if(shouldBeRemoved(aValue)) { it.remove(); } }

After the quick-fix is applied:

collection.removeIf(aValue -> shouldBeRemoved(aValue));

This inspection depends on the Java feature 'Lambda methods in collections' which is available since Java 8.

Inspection Details

Available in:

IntelliJ IDEA 2024.2, Qodana for JVM 2024.2,

Plugin:

Java, 242.SNAPSHOT

Last modified: 20 February 2024