diff --git a/core-java-collections/src/main/java/com/baeldung/java/list/ReverseIterator.java b/core-java-collections/src/main/java/com/baeldung/java/list/ReverseIterator.java new file mode 100644 index 0000000000..42dd543ed4 --- /dev/null +++ b/core-java-collections/src/main/java/com/baeldung/java/list/ReverseIterator.java @@ -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 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 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 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 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 list) { + + final List reversedList = Lists.reverse(list); + for (final String item : reversedList) { + System.out.println(item); + } + } + +} diff --git a/core-java-collections/src/test/java/com/baeldung/java/list/ReverseIteratorUnitTest.java b/core-java-collections/src/test/java/com/baeldung/java/list/ReverseIteratorUnitTest.java new file mode 100644 index 0000000000..172a2fe417 --- /dev/null +++ b/core-java-collections/src/test/java/com/baeldung/java/list/ReverseIteratorUnitTest.java @@ -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 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 reversedList = Lists.reverse(list); + for (final String item : reversedList) { + reverseString += item; + } + assertEquals(reverseString, StringUtils.reverse(originalString)); + + assertEquals("A", list.get(0)); + } +} \ No newline at end of file