Iterating Backward Through a List – 向后迭代一个列表

最后修改: 2018年 7月 3日

中文/混合/英文(键盘快捷键:t)

1. Overview

1.概述

In this quick tutorial, we’ll learn about various ways in which we can iterate backward through a list in Java.

在这个快速教程中,我们将学习在Java中向后迭代一个列表的各种方法。

2. Iterator in Java

2.Java中的迭代器

An Iterator is an interface in the Java Collections Framework that allows us to iterate over the elements in a collection. It was introduced in Java 1.2 as a replacement of Enumeration

IteratorJava Collections Framework中的一个接口,它允许我们对集合中的元素进行迭代。它是在 Java 1.2 中引入的,作为 枚举的替代。

3. Iterating Backward Using Core Java

3.使用Core Java向后迭代

3.1. Reversed for Loop

3.1.反转的for循环

The most simple implementation is to use a for loop to start from the last element of the list, and decrement the index as we reach the beginning of the list:

最简单的实现是使用一个for 循环,从列表的最后一个元素开始,当我们到达列表的开头时递减索引

for (int i = list.size(); i-- > 0; ) {
    System.out.println(list.get(i));
}

3.2. ListIterator

3.2.ListIterator

We can use a ListIterator to iterate over the elements in the list.

我们可以使用一个ListIterator来迭代列表中的元素。

Providing the size of the list as an index to the ListIterator will give us an iterator pointing to the end of the list:

提供列表的大小作为ListIterator的索引将给我们一个指向列表末端的迭代器。

ListIterator listIterator = list.listIterator(list.size());

This iterator now allows us to traverse the list in the reverse direction:

这个迭代器现在允许我们以相反的方向遍历列表。

while (listIterator.hasPrevious()) {
    System.out.println(listIterator.previous());
}

3.3. Collections.reverse()

3.3.Collections.reverse()

The Collections class in Java provides a static method to reverse the order of elements in a specified list:

Java中的Collections类提供了一个静态方法来扭转指定列表中元素的顺序:

Collections.reverse(list);

The reversed list can then be used to iterate backward over the original elements:

然后,反转的列表可以被用来向后迭代原始元素。

for (String item : list) {
    System.out.println(item);
}

This method, however, reverses the actual list by changing the order of elements in-place, and may not be desirable in many cases.

然而,这种方法通过就地改变元素的顺序来颠覆实际的列表,在许多情况下可能不可取。

4. Iterating Backwards Using Apache’s ReverseListIterator

4.使用Apache的ReverseListIterator进行逆向迭代

The Apache Commons Collections library has a nice ReverseListIterator class that allows us to loop through the elements in a list without actually reversing it.

Apache Commons Collections库有一个很好的ReverseListIterator类,它允许我们循环浏览一个列表中的元素,而不需要实际逆转它。

Before we start, we need to import the latest dependencies from Maven Central:

在开始之前,我们需要从Maven Central导入最新的依赖项。

<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-collections4</artifactId>
    <version>4.1</version>
</dependency>

We can create a new ReverseListIterator by passing the original list as a constructor parameter:

我们可以通过传递原始列表作为构造函数参数来创建一个新的ReverseListIterator

ReverseListIterator reverseListIterator = new ReverseListIterator(list);

We can then use this iterator to traverse the list backward:

然后我们可以使用这个迭代器来向后遍历这个列表。

while (reverseListIterator.hasNext()) {
    System.out.println(reverseListIterator.next());
}

5. Iterating Backwards Using Guava’s Lists.reverse()

5.使用Guava的Lists.reverse()进行逆向迭代

Similarly, the Google Guava library also provides a static reverse() method in its Lists class that returns a reverse view of the provided list.

同样,Google Guava库也在其Lists类中提供了一个静态reverse()方法,以返回所提供列表的反向视图。

Latest Guava version can be found over on Maven Central:

最新的Guava版本可以在Maven Central上找到。

<dependency>
    <groupId>com.google.guava</groupId>
    <artifactId>guava</artifactId>
    <version>31.0.1-jre</version>
</dependency>

Invoking the static method reverse() on the Lists class gives us the list in a reversed way:

Lists类上调用静态方法reverse(),可以得到反转的列表。

List<String> reversedList = Lists.reverse(list);

The reversed list can then be used to iterate backward over the original list:

然后,反转的列表可以用来在原始列表上向后迭代。

for (String item : reversedList) {
    System.out.println(item);
}

This method returns a new list with the elements of the original list in reversed order.

这个方法返回一个新的列表,其中的元素与原列表的顺序相反

6. Conclusion

6.结论

In this article, we’ve looked at different ways of iterating backward through a list in Java. We went through some examples using core Java, as well as using popular third-party libraries.

在这篇文章中,我们研究了在Java中通过列表向后迭代的不同方法。我们经历了一些使用核心Java的例子,以及使用流行的第三方库。

The source code for this article and the relevant test cases are available over on GitHub.

本文的源代码和相关的测试案例可以在GitHub上找到