Given one applies the clean-up Convert to enhanced 'for' loops, without the option only if the loop variable is used, to the following code, then the afterwards unused import for java.util.Iterator is not removed.
package test;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
public class Test {
private void method(List<String> list, Map<Date, List<String>> map) {
Iterator<String> removed = list.iterator();
while (removed.hasNext()) {
System.out.println(removed.next());
}
for (Iterator<List<String>> value = map.values().iterator(); value.hasNext();) {
System.out.println(value.next());
}
}
}
Is cleaned up to:
package test;
import java.util.Date;
import java.util.Iterator; // <-- Is now unused
import java.util.List;
import java.util.Map;
public class Test {
private void method(List<String> list, Map<Date, List<String>> map) {
for (String element : list) {
System.out.println(element);
}
for (List<String> list2 : map.values()) {
System.out.println(list2);
}
}
}
If the initial code only contains either of the for or while loop the afterwards unused import is removed correctly.