Java 8 Recipes
Java 8 Recipes
Ken Kousen
NFJS
Contact Info
Ken Kousen
Kousen IT, Inc.
[email protected]
http://www.kousenit.com
http://kousenit.wordpress.com (blog)
@kenkousen
https://github.com/kousen (repo)
Publications
Parameters may be in ()
Expression may be in { }
Lambda Expressions
Parameter types inferred from use
Lambda Expressions
Parameter types inferred from use
@FunctionalInterface
Not required, but useful
Functional Interfaces
Consumer → single arg, no result
void accept(T t)
Functional Interfaces
Consumer → single arg, no result
void accept(T t)
Predicate → returns boolean
boolean test(T t)
Functional Interfaces
Consumer → single arg, no result
void accept(T t)
Predicate → returns boolean
boolean test(T t)
Supplier → returns single result
T get()
Functional Interfaces
Consumer → single arg, no result
void accept(T t)
Predicate → returns boolean
boolean test(T t)
Supplier → no arg, returns single result
T get()
Function → single arg, returns result
R apply(T t)
Functional Interfaces
Often primitive variations
Consumer
IntConsumer, LongConsumer,
DoubleConsumer
BiConsumer<T,U>
Functional Interfaces
BiFunction → binary function from T and U to R
R apply(T, U)
Either
Catch others in body of lambda
Define your own interface with exceptions
Method References
Method references use :: notation
System.out::println
x → System.out.println(x)
Math::max
(x,y) → Math.max(x,y)
String::compareToIgnoreCase
(x,y) → x.compareToIgnoreCase(y)
Method References
Three general forms:
object::instanceMethod
Class::staticMethod
Class::instanceMethod
Constructor References
Can call constructors
MyClass::new
MyClass[]::new
Closure Variables
Variables in scope can be used in lambdas
Interface vs Interface →
Child overrides parent
Otherwise compiler error
Static methods in interfaces
Can add static methods to interfaces
See Comparator.comparing
Streams
A sequence of elements
Does not store the elements
Does not change the source
Operations are lazy when possible
Streams
Use the stream() method on collections
(It's a default method in Collection)
A source
Zero or more intermediate operations
A terminal operation
Reduction Operations
Reduction operations
Terminal operations that produce
one value from a stream
Wait, what?
Stream.generate(Math::random)
.limit(100)
Sorting
Collections.sort(...)
destructive; sorts in place
Stream.sorted(...)
returns a new sorted stream
Streams
Streams are lazy
computation on source only when
terminal operation initiated
Return an Optional
Optional
Alternative to returning object or null
Optional<T> value
isPresent() → boolean
get() → return the value
LocalDate.of(2015, 2, 2)
months actually count from 1 now
LocalTime
LocalTime is just LocalDate for times
hh:mm:ss
ZoneId.getAvailableIds()
ZoneId.of("... tz name …")
ZonedDateTime
LocalDateTime → ZonedDateTime
local.atZone(zoneId)
Instant → ZonedDateTime
instant.atZone(ZoneId.of("UTC"))
Concurrency
Atomic classes from Java 5
AtomicInteger, AtomicLong
new methods like
updateAndGet
accumulateAndGet
Miscellaneous
Nashorn
JavaFX
Conclusions
They remade the whole language
lambdas
streams
method references
functional interfaces
date/time API
concurrency, and more...