Add java8-features module: runnable demos and captured output for the ankurm.com java8-features article
This commit is contained in:
@@ -0,0 +1,7 @@
|
||||
$ javac src/LambdaDemo.java && java LambdaDemo (1.8.0_504)
|
||||
anonymous class : [Zoya, Meera, Ankur, Bhaskar]
|
||||
lambda : [Ankur, Bhaskar, Meera, Zoya]
|
||||
method reference: [Ankur, Bhaskar, Meera, Zoya]
|
||||
captured base : 15
|
||||
lambda this : LambdaDemo
|
||||
anonymous this : LambdaDemo$2
|
||||
@@ -0,0 +1,8 @@
|
||||
$ javac src/StreamsDemo.java && java StreamsDemo (1.8.0_504)
|
||||
distinct+upper : [STREAM, LAMBDA, OPTIONAL, DEFAULT, TIME]
|
||||
sum of lengths : 37
|
||||
pipeline built, nothing printed yet
|
||||
filtering stream
|
||||
first match : stream
|
||||
reuse : stream has already been operated upon or closed
|
||||
groupingBy : {long=[stream, lambda, optional, default], short=[time]}
|
||||
@@ -0,0 +1,8 @@
|
||||
$ javac src/OptionalDemo.java && java OptionalDemo (1.8.0_504)
|
||||
present : VALUE-1
|
||||
absent : none
|
||||
orElse (value present):
|
||||
expensive() called
|
||||
orElseGet (value present):
|
||||
get() on empty : No value present
|
||||
Optional.of(null): NullPointerException
|
||||
@@ -0,0 +1,3 @@
|
||||
$ javac src/DefaultMethodsDemo.java && java DefaultMethodsDemo (1.8.0_504)
|
||||
diamond resolved: walk + swim
|
||||
default + static: Hello, Ankur
|
||||
@@ -0,0 +1,8 @@
|
||||
$ javac src/TimeDemo.java && java TimeDemo (1.8.0_504)
|
||||
plusMonths : 2026-11-20 (original still 2026-10-20)
|
||||
end of month : 2026-02-28
|
||||
days between : 292
|
||||
Period : P6Y7M21D
|
||||
Duration : PT2H15M
|
||||
same instant NY : 2026-10-20 13:05 EDT
|
||||
old Date month : new java.util.Date(126, 9, 20) -> month index 9 (October is 9)
|
||||
@@ -0,0 +1,7 @@
|
||||
$ javac src/FunctionalInterfacesDemo.java && java FunctionalInterfacesDemo (1.8.0_504)
|
||||
andThen : 11
|
||||
compose : 12
|
||||
negate : true
|
||||
Supplier : supplied
|
||||
BiFunction : 7
|
||||
UnaryOperator : OK
|
||||
@@ -0,0 +1,11 @@
|
||||
$ javac broken/CaptureError.java (1.8.0_504)
|
||||
broken/CaptureError.java:7: error: local variables referenced from a lambda expression must be final or effectively final
|
||||
Supplier<Integer> s = () -> counter;
|
||||
^
|
||||
1 error
|
||||
|
||||
$ javac broken/DiamondError.java (1.8.0_504)
|
||||
broken/DiamondError.java:5: error: class C inherits unrelated defaults for move() from types A and B
|
||||
static class C implements A, B { }
|
||||
^
|
||||
1 error
|
||||
Reference in New Issue
Block a user