From 900ff584fea9d67c9746dbed7e6aeea76d2bfb52 Mon Sep 17 00:00:00 2001 From: Kai Yuan Date: Sat, 20 Jan 2024 16:59:41 +0100 Subject: [PATCH] [reset-listiterator] reset listiterator (#15645) --- .../ResetListIteratorUnitTest.java | 46 +++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 core-java-modules/core-java-collections-list-6/src/test/java/com/baeldung/resetlistiterator/ResetListIteratorUnitTest.java diff --git a/core-java-modules/core-java-collections-list-6/src/test/java/com/baeldung/resetlistiterator/ResetListIteratorUnitTest.java b/core-java-modules/core-java-collections-list-6/src/test/java/com/baeldung/resetlistiterator/ResetListIteratorUnitTest.java new file mode 100644 index 0000000000..2b033e3061 --- /dev/null +++ b/core-java-modules/core-java-collections-list-6/src/test/java/com/baeldung/resetlistiterator/ResetListIteratorUnitTest.java @@ -0,0 +1,46 @@ +package com.baeldung.resetlistiterator; + +import org.junit.jupiter.api.Test; + +import java.util.List; +import java.util.ListIterator; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; + +public class ResetListIteratorUnitTest { + + private static final List MY_LIST = List.of("A", "B", "C", "D", "E", "F", "G"); + + @Test + void whenRecreateAnListIterator_thenGetTheExpectedResult() { + ListIterator lit = MY_LIST.listIterator(); + lit.next(); + lit.next(); + lit.next(); + lit.next(); + + lit = MY_LIST.listIterator(); + + assertFalse(lit.hasPrevious()); + assertEquals("A", lit.next()); + } + + @Test + void whenBackwardIterationToTheFirst_thenGetTheExpectedResult() { + ListIterator lit = MY_LIST.listIterator(); + lit.next(); + lit.next(); + lit.next(); + lit.next(); + + + while (lit.hasPrevious()) { + lit.previous(); + } + + assertFalse(lit.hasPrevious()); + assertEquals("A", lit.next()); + } + +} \ No newline at end of file