MCPcopy Index your code
hub / github.com/BruceEckel/OnJava8-Examples / main

Method main

streams/LastElement.java:9–19  ·  view source on GitHub ↗
(String[] args)

Source from the content-addressed store, hash-verified

7
8public 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:
2219

Callers

nothing calls this directly

Calls 2

rangeMethod · 0.80
orElseMethod · 0.80

Tested by

no test coverage detected