diff --git a/src/main/java/org/springframework/data/support/WindowIterator.java b/src/main/java/org/springframework/data/support/WindowIterator.java index 7af2a9ae34..4dad3dc993 100644 --- a/src/main/java/org/springframework/data/support/WindowIterator.java +++ b/src/main/java/org/springframework/data/support/WindowIterator.java @@ -15,8 +15,9 @@ */ package org.springframework.data.support; -import java.util.Collections; import java.util.Iterator; +import java.util.List; +import java.util.ListIterator; import java.util.NoSuchElementException; import java.util.function.Function; @@ -83,7 +84,7 @@ public boolean hasNext() { if (currentIterator == null) { if (currentWindow != null) { currentIterator = isBackwardsScrolling(currentPosition) - ? currentWindow.stream().sorted(Collections.reverseOrder()).iterator() + ? new ReverseListIterator<>(currentWindow.getContent()) : currentWindow.iterator(); } } @@ -161,4 +162,28 @@ public WindowIterator startingAt(ScrollPosition position) { return new WindowIterator<>(windowFunction, position); } } + + private static class ReverseListIterator implements Iterator { + + private final ListIterator delegate; + + public ReverseListIterator(List list) { + this.delegate = list.listIterator(list.size()); + } + + @Override + public boolean hasNext() { + return delegate.hasPrevious(); + } + + @Override + public T next() { + return delegate.previous(); + } + + @Override + public void remove() { + delegate.remove(); + } + } }