diff --git a/src/main/java/org/apache/commons/collections4/CollectionUtils.java b/src/main/java/org/apache/commons/collections4/CollectionUtils.java index 5993ce15c..28b5f3a67 100644 --- a/src/main/java/org/apache/commons/collections4/CollectionUtils.java +++ b/src/main/java/org/apache/commons/collections4/CollectionUtils.java @@ -432,7 +432,6 @@ public class CollectionUtils { if (coll2.isEmpty()) { return true; } - final Iterator it = coll1.iterator(); final Set elementsAlreadySeen = new HashSet<>(); for (final Object nextElement : coll2) { if (elementsAlreadySeen.contains(nextElement)) { @@ -440,8 +439,7 @@ public class CollectionUtils { } boolean foundCurrentElement = false; - while (it.hasNext()) { - final Object p = it.next(); + for (final Object p : coll1) { elementsAlreadySeen.add(p); if (Objects.equals(nextElement, p)) { foundCurrentElement = true; diff --git a/src/test/java/org/apache/commons/collections4/MapUtilsTest.java b/src/test/java/org/apache/commons/collections4/MapUtilsTest.java index ac22b5740..9b4f0cecd 100644 --- a/src/test/java/org/apache/commons/collections4/MapUtilsTest.java +++ b/src/test/java/org/apache/commons/collections4/MapUtilsTest.java @@ -35,7 +35,6 @@ import java.util.ArrayList; import java.util.Collection; import java.util.HashMap; import java.util.HashSet; -import java.util.Iterator; import java.util.List; import java.util.ListResourceBundle; import java.util.Map; @@ -274,10 +273,8 @@ public class MapUtilsTest { @Override public Object[][] getContents() { final Object[][] contents = new Object[in.size()][2]; - final Iterator i = in.keySet().iterator(); int n = 0; - while (i.hasNext()) { - final Object key = i.next(); + for (final Object key : in.keySet()) { final Object val = in.get(key); contents[n][0] = key; contents[n][1] = val; diff --git a/src/test/java/org/apache/commons/collections4/bidimap/DualTreeBidiMap2Test.java b/src/test/java/org/apache/commons/collections4/bidimap/DualTreeBidiMap2Test.java index 40bee2697..7b64e4460 100644 --- a/src/test/java/org/apache/commons/collections4/bidimap/DualTreeBidiMap2Test.java +++ b/src/test/java/org/apache/commons/collections4/bidimap/DualTreeBidiMap2Test.java @@ -137,9 +137,7 @@ public class DualTreeBidiMap2Test, V extends Comparable< newSortedKeys = Collections.unmodifiableList(newSortedKeys); final Iterator mapIter = sm.keySet().iterator(); - final Iterator expectedIter = newSortedKeys.iterator(); - while (expectedIter.hasNext()) { - final K expectedKey = expectedIter.next(); + for (final K expectedKey : newSortedKeys) { final K mapKey = mapIter.next(); assertNotNull("key in sorted list may not be null", expectedKey); assertNotNull("key in map may not be null", mapKey); diff --git a/src/test/java/org/apache/commons/collections4/list/AbstractListTest.java b/src/test/java/org/apache/commons/collections4/list/AbstractListTest.java index 37e9110c0..d1c3bdf65 100644 --- a/src/test/java/org/apache/commons/collections4/list/AbstractListTest.java +++ b/src/test/java/org/apache/commons/collections4/list/AbstractListTest.java @@ -91,12 +91,10 @@ public abstract class AbstractListTest extends AbstractCollectionTest { int i = 0; final Iterator iterator1 = list1.iterator(); - final Iterator iterator2 = list2.iterator(); final E[] array = (E[]) list1.toArray(); - while (iterator2.hasNext()) { + for (Object o2 : list2) { assertTrue("List iterator should have next", iterator1.hasNext()); final Object o1 = iterator1.next(); - Object o2 = iterator2.next(); assertEquals("Iterator elements should be equal", o1, o2); o2 = list1.get(i); assertEquals("get should return correct element", o1, o2); @@ -428,9 +426,7 @@ public abstract class AbstractListTest extends AbstractCollectionTest { final List list1 = getCollection(); final List list2 = getConfirmed(); - final Iterator iterator = list2.iterator(); - while (iterator.hasNext()) { - final E element = iterator.next(); + for (final E element : list2) { assertEquals("lastIndexOf should return correct result", list1.lastIndexOf(element), list2.lastIndexOf(element)); verify(); diff --git a/src/test/java/org/apache/commons/collections4/map/AbstractMapTest.java b/src/test/java/org/apache/commons/collections4/map/AbstractMapTest.java index 0fbf5a573..2ac4b064d 100644 --- a/src/test/java/org/apache/commons/collections4/map/AbstractMapTest.java +++ b/src/test/java/org/apache/commons/collections4/map/AbstractMapTest.java @@ -1646,10 +1646,8 @@ public abstract class AbstractMapTest extends AbstractObjectTest { @Test public void testMapEntrySetIteratorEntry() { resetFull(); - final Iterator> it = getCollection().iterator(); int count = 0; - while (it.hasNext()) { - final Map.Entry entry = it.next(); + for (final Entry entry : getCollection()) { assertTrue(AbstractMapTest.this.getMap().containsKey(entry.getKey())); assertTrue(AbstractMapTest.this.getMap().containsValue(entry.getValue())); if (!isGetStructuralModify()) { diff --git a/src/test/java/org/apache/commons/collections4/multimap/AbstractMultiValuedMapTest.java b/src/test/java/org/apache/commons/collections4/multimap/AbstractMultiValuedMapTest.java index e4b7e69b0..b7f4578f3 100644 --- a/src/test/java/org/apache/commons/collections4/multimap/AbstractMultiValuedMapTest.java +++ b/src/test/java/org/apache/commons/collections4/multimap/AbstractMultiValuedMapTest.java @@ -428,9 +428,7 @@ public abstract class AbstractMultiValuedMapTest extends AbstractObjectTes public void testEntriesCollectionIterator() { final MultiValuedMap map = makeFullMap(); final Collection values = new ArrayList<>(map.values()); - final Iterator> iterator = map.entries().iterator(); - while (iterator.hasNext()) { - final Map.Entry entry = iterator.next(); + for (final Entry entry : map.entries()) { assertTrue(map.containsMapping(entry.getKey(), entry.getValue())); assertTrue(values.contains(entry.getValue())); if (isRemoveSupported()) {