for-loops changed to while-loops when appropiate (#2163)
This commit is contained in:
parent
50ad7320a3
commit
d0c7340da8
|
@ -1,5 +1,10 @@
|
||||||
package com.baeldung.commons.collections.orderedmap;
|
package com.baeldung.commons.collections.orderedmap;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
import org.apache.commons.collections4.OrderedMap;
|
import org.apache.commons.collections4.OrderedMap;
|
||||||
import org.apache.commons.collections4.OrderedMapIterator;
|
import org.apache.commons.collections4.OrderedMapIterator;
|
||||||
import org.apache.commons.collections4.map.LinkedMap;
|
import org.apache.commons.collections4.map.LinkedMap;
|
||||||
|
@ -7,11 +12,6 @@ import org.apache.commons.collections4.map.ListOrderedMap;
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
import static org.junit.Assert.assertEquals;
|
|
||||||
|
|
||||||
public class OrderedMapUnitTest {
|
public class OrderedMapUnitTest {
|
||||||
|
|
||||||
private String[] names = {"Emily", "Mathew", "Rose", "John", "Anna"};
|
private String[] names = {"Emily", "Mathew", "Rose", "John", "Anna"};
|
||||||
|
@ -45,11 +45,12 @@ public class OrderedMapUnitTest {
|
||||||
// as defined in the constant arrays of names and ages:
|
// as defined in the constant arrays of names and ages:
|
||||||
|
|
||||||
OrderedMapIterator<String, Integer> runnersIterator = this.runnersLinkedMap.mapIterator();
|
OrderedMapIterator<String, Integer> runnersIterator = this.runnersLinkedMap.mapIterator();
|
||||||
for (int i = 0; runnersIterator.hasNext(); i++) {
|
int i = 0;
|
||||||
|
while (runnersIterator.hasNext()) {
|
||||||
runnersIterator.next();
|
runnersIterator.next();
|
||||||
|
|
||||||
assertEquals(runnersIterator.getKey(), this.names[i]);
|
assertEquals(runnersIterator.getKey(), this.names[i]);
|
||||||
assertEquals(runnersIterator.getValue(), this.ages[i]);
|
assertEquals(runnersIterator.getValue(), this.ages[i]);
|
||||||
|
i++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -59,11 +60,12 @@ public class OrderedMapUnitTest {
|
||||||
// as defined in the constant arrays of names and ages:
|
// as defined in the constant arrays of names and ages:
|
||||||
|
|
||||||
OrderedMapIterator<String, Integer> runnersIterator = this.runnersListOrderedMap.mapIterator();
|
OrderedMapIterator<String, Integer> runnersIterator = this.runnersListOrderedMap.mapIterator();
|
||||||
for (int i = 0; runnersIterator.hasNext(); i++) {
|
int i = 0;
|
||||||
|
while (runnersIterator.hasNext()) {
|
||||||
runnersIterator.next();
|
runnersIterator.next();
|
||||||
|
|
||||||
assertEquals(runnersIterator.getKey(), this.names[i]);
|
assertEquals(runnersIterator.getKey(), this.names[i]);
|
||||||
assertEquals(runnersIterator.getValue(), this.ages[i]);
|
assertEquals(runnersIterator.getValue(), this.ages[i]);
|
||||||
|
i++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -73,9 +75,11 @@ public class OrderedMapUnitTest {
|
||||||
// as defined in the constant arrays of names and ages
|
// as defined in the constant arrays of names and ages
|
||||||
|
|
||||||
String name = this.runnersLinkedMap.firstKey();
|
String name = this.runnersLinkedMap.firstKey();
|
||||||
for (int i = 0; name != null; i++) {
|
int i = 0;
|
||||||
|
while (name != null) {
|
||||||
assertEquals(name, this.names[i]);
|
assertEquals(name, this.names[i]);
|
||||||
name = this.runnersLinkedMap.nextKey(name);
|
name = this.runnersLinkedMap.nextKey(name);
|
||||||
|
i++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -85,9 +89,11 @@ public class OrderedMapUnitTest {
|
||||||
// as defined in the constant arrays of names and ages
|
// as defined in the constant arrays of names and ages
|
||||||
|
|
||||||
String name = this.runnersListOrderedMap.firstKey();
|
String name = this.runnersListOrderedMap.firstKey();
|
||||||
for (int i = 0; name != null; i++) {
|
int i = 0;
|
||||||
|
while (name != null) {
|
||||||
assertEquals(name, this.names[i]);
|
assertEquals(name, this.names[i]);
|
||||||
name = this.runnersListOrderedMap.nextKey(name);
|
name = this.runnersListOrderedMap.nextKey(name);
|
||||||
|
i++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -97,9 +103,11 @@ public class OrderedMapUnitTest {
|
||||||
// as defined in the constant arrays of names and ages
|
// as defined in the constant arrays of names and ages
|
||||||
|
|
||||||
String name = this.runnersLinkedMap.lastKey();
|
String name = this.runnersLinkedMap.lastKey();
|
||||||
for (int i = RUNNERS_COUNT - 1; name != null; i--) {
|
int i = RUNNERS_COUNT - 1;
|
||||||
|
while (name != null) {
|
||||||
assertEquals(name, this.names[i]);
|
assertEquals(name, this.names[i]);
|
||||||
name = this.runnersLinkedMap.previousKey(name);
|
name = this.runnersLinkedMap.previousKey(name);
|
||||||
|
i--;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -109,9 +117,11 @@ public class OrderedMapUnitTest {
|
||||||
// as defined in the constant arrays of names and ages
|
// as defined in the constant arrays of names and ages
|
||||||
|
|
||||||
String name = this.runnersListOrderedMap.lastKey();
|
String name = this.runnersListOrderedMap.lastKey();
|
||||||
for (int i = RUNNERS_COUNT - 1; name != null; i--) {
|
int i = RUNNERS_COUNT - 1;
|
||||||
|
while (name != null) {
|
||||||
assertEquals(name, this.names[i]);
|
assertEquals(name, this.names[i]);
|
||||||
name = this.runnersListOrderedMap.previousKey(name);
|
name = this.runnersListOrderedMap.previousKey(name);
|
||||||
|
i--;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue