In a sorted stream, I'd like to access the previous element.
I'm looking for something like this:
class Main {
public static void main(String[] args) {
List<String> stringList = Arrays.asList("1: First", "2: Second", "3: Third", "4: Fourth");
List<String> resultList = stringList.stream()
.sorted()
.filter(s -> s.contains("Second") && s.previous().contains("First"))
^^^^^^^^^^^^ doesn't compile
.collect(Collectors.toList());
System.out.println(resultList);
}
}
Expected Output in this case is
Second
Important: I'd like to factor out the lambda in the filter()
method to reuse it in different places.