Skip to content

Commit 6ca3170

Browse files
committed
Polishing.
Use ReverseListIterator instead of Stream API to reduce overhead. ListIterator provides means to iterate backwards so we're wrapping the existing iterator. See #2857. Original pull request: #2858.
1 parent d156819 commit 6ca3170

File tree

1 file changed

+27
-2
lines changed

1 file changed

+27
-2
lines changed

src/main/java/org/springframework/data/support/WindowIterator.java

+27-2
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,9 @@
1515
*/
1616
package org.springframework.data.support;
1717

18-
import java.util.Collections;
1918
import java.util.Iterator;
19+
import java.util.List;
20+
import java.util.ListIterator;
2021
import java.util.NoSuchElementException;
2122
import java.util.function.Function;
2223

@@ -83,7 +84,7 @@ public boolean hasNext() {
8384
if (currentIterator == null) {
8485
if (currentWindow != null) {
8586
currentIterator = isBackwardsScrolling(currentPosition)
86-
? currentWindow.stream().sorted(Collections.reverseOrder()).iterator()
87+
? new ReverseListIterator<>(currentWindow.getContent())
8788
: currentWindow.iterator();
8889
}
8990
}
@@ -161,4 +162,28 @@ public WindowIterator<T> startingAt(ScrollPosition position) {
161162
return new WindowIterator<>(windowFunction, position);
162163
}
163164
}
165+
166+
private static class ReverseListIterator<T> implements Iterator<T> {
167+
168+
private final ListIterator<T> delegate;
169+
170+
public ReverseListIterator(List<T> list) {
171+
this.delegate = list.listIterator(list.size());
172+
}
173+
174+
@Override
175+
public boolean hasNext() {
176+
return delegate.hasPrevious();
177+
}
178+
179+
@Override
180+
public T next() {
181+
return delegate.previous();
182+
}
183+
184+
@Override
185+
public void remove() {
186+
delegate.remove();
187+
}
188+
}
164189
}

0 commit comments

Comments
 (0)