I want to display the second element of the stream for which the name starts with 's'.
I tried:
employees.stream()
.filter(e -> e.getName().charAt(0) == 's')
.findAny()
.ifPresent(e -> System.out.println("Employee : " + e));
However, when I use findAny()
, it returns the first element in the stream (same as findFirst()
) and I want the second one.
You can skip the first match by adding skip(1)
after the filter:
employees.stream()
.filter(e -> e.getName().charAt(0) == 's')
.skip(1)
.findAny()
.ifPresent(e -> System.out.println("Employee : " + e));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With