[BAEL-1847] Iterate Backwards through a List (#4540)

* [BAEL-1847] Iterate Backwards through a List

* [BAEL-1847] Updated unit tests
This commit is contained in:
Siben Nayak 2018-07-01 16:11:31 +05:30 committed by Predrag Maric
parent bafa1678eb
commit 92340e9ca5
2 changed files with 167 additions and 0 deletions

View File

@ -0,0 +1,80 @@
package com.baeldung.java.list;
import java.util.Collections;
import java.util.List;
import java.util.ListIterator;
import org.apache.commons.collections4.iterators.ReverseListIterator;
import com.google.common.collect.Lists;
/**
* Provides methods for iterating backward over a list.
*/
public class ReverseIterator {
/**
* Iterate using the for loop.
*
* @param list the list
*/
public void iterateUsingForLoop(final List<String> list) {
for (int i = list.size(); i-- > 0; ) {
System.out.println(list.get(i));
}
}
/**
* Iterate using the Java {@link ListIterator}.
*
* @param list the list
*/
public void iterateUsingListIterator(final List<String> list) {
final ListIterator listIterator = list.listIterator(list.size());
while (listIterator.hasPrevious()) {
System.out.println(listIterator.previous());
}
}
/**
* Iterate using Java {@link Collections} API.
*
* @param list the list
*/
public void iterateUsingCollections(final List<String> list) {
Collections.reverse(list);
for (final String item : list) {
System.out.println(item);
}
}
/**
* Iterate using Apache Commons {@link ReverseListIterator}.
*
* @param list the list
*/
public void iterateUsingApacheReverseListIterator(final List<String> list) {
final ReverseListIterator listIterator = new ReverseListIterator(list);
while (listIterator.hasNext()) {
System.out.println(listIterator.next());
}
}
/**
* Iterate using Guava {@link Lists} API.
*
* @param list the list
*/
public void iterateUsingGuava(final List<String> list) {
final List<String> reversedList = Lists.reverse(list);
for (final String item : reversedList) {
System.out.println(item);
}
}
}

View File

@ -0,0 +1,87 @@
package com.baeldung.java.list;
import static org.junit.Assert.assertEquals;
import java.util.Collections;
import java.util.List;
import java.util.ListIterator;
import org.apache.commons.collections4.iterators.ReverseListIterator;
import org.apache.commons.lang3.StringUtils;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import com.google.common.collect.Lists;
public class ReverseIteratorUnitTest {
private final ReverseIterator reverseIterator = new ReverseIterator();
private List<String> list;
private final String originalString = "ABCDE";
@BeforeEach
void setUp() {
list = Lists.newArrayList("A", "B", "C", "D", "E");
}
@Test
void whenIteratingUsingForLoop_thenCorrect() {
String reverseString = "";
for (int i = list.size(); i-- > 0; ) {
reverseString += list.get(i);
}
assertEquals(reverseString, StringUtils.reverse(originalString));
}
@Test
void whenIteratingUsingListIterator_thenCorrect() {
String reverseString = "";
final ListIterator listIterator = list.listIterator(list.size());
while (listIterator.hasPrevious()) {
reverseString += listIterator.previous();
}
assertEquals(reverseString, StringUtils.reverse(originalString));
}
@Test
void whenIteratingUsingCollections_thenCorrect() {
String reverseString = "";
Collections.reverse(list);
for (final String item : list) {
reverseString += item;
}
assertEquals(reverseString, StringUtils.reverse(originalString));
assertEquals("E", list.get(0));
}
@Test
void whenIteratingUsingApacheReverseListIterator_thenCorrect() {
String reverseString = "";
final ReverseListIterator listIterator = new ReverseListIterator(list);
while (listIterator.hasNext()) {
reverseString += listIterator.next();
}
assertEquals(reverseString, StringUtils.reverse(originalString));
}
@Test
void whenIteratingUsingGuava_thenCorrect() {
String reverseString = "";
final List<String> reversedList = Lists.reverse(list);
for (final String item : reversedList) {
reverseString += item;
}
assertEquals(reverseString, StringUtils.reverse(originalString));
assertEquals("A", list.get(0));
}
}