Skip to content

Commit d0c7340

Browse files
Blue Montag SoftwareKevinGilmore
authored andcommitted
for-loops changed to while-loops when appropiate (eugenp#2163)
1 parent 50ad732 commit d0c7340

1 file changed

Lines changed: 23 additions & 13 deletions

File tree

‎libraries/src/test/java/com/baeldung/commons/collections/orderedmap/OrderedMapUnitTest.java‎

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
package com.baeldung.commons.collections.orderedmap;
22

3+
import static org.junit.Assert.assertEquals;
4+
5+
import java.util.ArrayList;
6+
import java.util.List;
7+
38
import org.apache.commons.collections4.OrderedMap;
49
import org.apache.commons.collections4.OrderedMapIterator;
510
import org.apache.commons.collections4.map.LinkedMap;
611
import org.apache.commons.collections4.map.ListOrderedMap;
712
import org.junit.Before;
813
import org.junit.Test;
914

10-
import java.util.ArrayList;
11-
import java.util.List;
12-
13-
import static org.junit.Assert.assertEquals;
14-
1515
public class OrderedMapUnitTest {
1616

1717
private String[] names = {"Emily", "Mathew", "Rose", "John", "Anna"};
@@ -45,11 +45,12 @@ public void givenALinkedMap_whenIteratedWithMapIterator_thenPreservesOrder() {
4545
// as defined in the constant arrays of names and ages:
4646

4747
OrderedMapIterator<String, Integer> runnersIterator = this.runnersLinkedMap.mapIterator();
48-
for (int i = 0; runnersIterator.hasNext(); i++) {
48+
int i = 0;
49+
while (runnersIterator.hasNext()) {
4950
runnersIterator.next();
50-
5151
assertEquals(runnersIterator.getKey(), this.names[i]);
5252
assertEquals(runnersIterator.getValue(), this.ages[i]);
53+
i++;
5354
}
5455
}
5556

@@ -59,11 +60,12 @@ public void givenAListOrderedMap_whenIteratedWithMapIterator_thenPreservesOrder(
5960
// as defined in the constant arrays of names and ages:
6061

6162
OrderedMapIterator<String, Integer> runnersIterator = this.runnersListOrderedMap.mapIterator();
62-
for (int i = 0; runnersIterator.hasNext(); i++) {
63+
int i = 0;
64+
while (runnersIterator.hasNext()) {
6365
runnersIterator.next();
64-
6566
assertEquals(runnersIterator.getKey(), this.names[i]);
6667
assertEquals(runnersIterator.getValue(), this.ages[i]);
68+
i++;
6769
}
6870
}
6971

@@ -73,9 +75,11 @@ public void givenALinkedMap_whenIteratedForwards_thenPreservesOrder() {
7375
// as defined in the constant arrays of names and ages
7476

7577
String name = this.runnersLinkedMap.firstKey();
76-
for (int i = 0; name != null; i++) {
78+
int i = 0;
79+
while (name != null) {
7780
assertEquals(name, this.names[i]);
7881
name = this.runnersLinkedMap.nextKey(name);
82+
i++;
7983
}
8084
}
8185

@@ -85,9 +89,11 @@ public void givenAListOrderedMap_whenIteratedForwards_thenPreservesOrder() {
8589
// as defined in the constant arrays of names and ages
8690

8791
String name = this.runnersListOrderedMap.firstKey();
88-
for (int i = 0; name != null; i++) {
92+
int i = 0;
93+
while (name != null) {
8994
assertEquals(name, this.names[i]);
9095
name = this.runnersListOrderedMap.nextKey(name);
96+
i++;
9197
}
9298
}
9399

@@ -97,9 +103,11 @@ public void givenALinkedMap_whenIteratedBackwards_thenPreservesOrder() {
97103
// as defined in the constant arrays of names and ages
98104

99105
String name = this.runnersLinkedMap.lastKey();
100-
for (int i = RUNNERS_COUNT - 1; name != null; i--) {
106+
int i = RUNNERS_COUNT - 1;
107+
while (name != null) {
101108
assertEquals(name, this.names[i]);
102109
name = this.runnersLinkedMap.previousKey(name);
110+
i--;
103111
}
104112
}
105113

@@ -109,9 +117,11 @@ public void givenAListOrderedMap_whenIteratedBackwards_thenPreservesOrder() {
109117
// as defined in the constant arrays of names and ages
110118

111119
String name = this.runnersListOrderedMap.lastKey();
112-
for (int i = RUNNERS_COUNT - 1; name != null; i--) {
120+
int i = RUNNERS_COUNT - 1;
121+
while (name != null) {
113122
assertEquals(name, this.names[i]);
114123
name = this.runnersListOrderedMap.previousKey(name);
124+
i--;
115125
}
116126
}
117127

0 commit comments

Comments
 (0)