Course – LS – All

Get started with Spring and Spring Boot, through the Learn Spring course:

>> CHECK OUT THE COURSE

1. Overview

When we work with Java, efficiently navigating through collections is a common requirement. When dealing with lists, the ListIterator interface provides a powerful tool for bidirectional traversal. However, there are situations where resetting the ListIterator to the first element becomes necessary.

In this tutorial, we will explore the various ways to reset a ListIterator to the beginning of a list in Java.

2. Introduction to the Problem

As usual, let’s understand the problem through an example.

Let’s say we have a list of strings:

List<String> MY_LIST = List.of("A", "B", "C", "D", "E", "F", "G");

Then, we can obtain a ListIterator on MY_LIST by MY_LIST.listIterator() and iterate through the list by calling ListIterator‘s next() method.

Sometimes, we may want to reset the ListIterator object by asking it to point to the position right before the first element in the list again, as it was newly created.

Next, we’ll look at different approaches to solving this problem. Also, we’ll leverage unit test assertions to verify whether each solution gives the expected result.

3. Creating a New ListIterator

We know that when we create a new ListIterator object, it points to the beginning of the target list. Therefore, the easiest way to reset a ListIterator instance would be to reassign the variable to a new ListIterator.

Let’s create a test and verify if this idea works as expected:

ListIterator<String> lit = MY_LIST.listIterator();
lit.next();
lit.next();
lit.next();
lit.next();

lit = MY_LIST.listIterator();

assertFalse(lit.hasPrevious());
assertEquals("A", lit.next());

As the test above shows, after we created a ListIterator lit, we called the lit.next() method four times. When we would like to reset lit, we created a new ListIterator instance and reassigned it to lit.

Then, we verify if lit is reset successfully by two assertions:

  • lit.hasPrevious() returns false
  • lit.next() should be the first element in MY_LIST (“A”)

If we give this test a run, it passes. Therefore, creating a new ListIterator solves our problem.

4. Iterating Backward to the Beginning of the List

Creating a new ListIterator can quickly navigate to the beginning of a list. However, we’ll have a new ListIterator object.

Sometimes, we want to keep the original ListIterator object and move its pointer back to the beginning of the target list. If this is the case, we can utilize ListIterator‘s bidirectional iteration feature to iterate backward to the beginning of the list.

Next, let’s create a test to see how to achieve that:

ListIterator<String> lit = MY_LIST.listIterator();
lit.next();
lit.next();
lit.next();
lit.next();

while (lit.hasPrevious()) {
    lit.previous();
}

assertFalse(lit.hasPrevious());
assertEquals("A", lit.next());

As we can see, we apply the backward iteration through a while loop.

If we run the test, it passes. So, it does the job.

It’s worth noting since this approach iterates from the current position backward to the beginning of the list, it can be slow if the number of elements in the list is significant.

5. Conclusion

In this article, we’ve explored two approaches to resetting a ListIterator to the beginning of the list.

If it’s required to keep the original ListIterator instance, we can iterate backward to the head of the list. Otherwise, creating a new ListIterator would be the most straightforward solution.

As always, the complete source code for the examples is available over on GitHub.

Course – LS – All

Get started with Spring and Spring Boot, through the Learn Spring course:

>> CHECK OUT THE COURSE
res – REST with Spring (eBook) (everywhere)
Comments are open for 30 days after publishing a post. For any issues past this date, use the Contact form on the site.