(String[] args)
| 7 | |
| 8 | public class LastElement { |
| 9 | public static void main(String[] args) { |
| 10 | OptionalInt last = IntStream.range(10, 20) |
| 11 | .reduce((n1, n2) -> n2); |
| 12 | System.out.println(last.orElse(-1)); |
| 13 | // Non-numeric object: |
| 14 | Optional<String> lastobj = |
| 15 | Stream.of("one", "two", "three") |
| 16 | .reduce((n1, n2) -> n2); |
| 17 | System.out.println( |
| 18 | lastobj.orElse("Nothing there!")); |
| 19 | } |
| 20 | } |
| 21 | /* Output: |
| 22 | 19 |