Why is this an issue?

Java 21 introduces the new Sequenced Collections API, which is applicable to all collections with a defined sequence on their elements, such as LinkedList, TreeSet, and others (see JEP 431). For projects using Java 21 and onwards, this API should be utilized instead of workaround implementations that were necessary before Java 21.

This rule reports when a collection is iterated in reverse through explicit implementation or workarounds, instead of using the reversed view of the collection.

How to fix it

Replace the reported statement with a forward-iteration over the reversed view of the collection.

Code examples

Noncompliant code example

void printLastToFirst(List<String> list) {
  for (var it = list.listIterator(list.size()); it.hasPrevious();) {
    var element = it.previous();
    System.out.println(element);
  }
}

Compliant solution

void printLastToFirst(List<String> list) {
  for (var element: list.reversed()) {
    System.out.println(element);
  }
}

Resources

Documentation