Java 8 Lambda Cheat Sheet
Even though Java 8 is just a little over six years old and brought a tremendous revolution to Java,
adoption of lambdas and functional approaches to programming problems is still an ongoing process.
I have introduced numerous people to functional-style programming & lambdas,
and at one point came up with the idea of a small “cheat sheet” only one or two DIN A4 pages in size that you could have
in hand when thinking about functional stuff. This is by no means meant to be a comprehensive introduction,
but a small reference document, and has been received very well, so I thought I’d share it here.
Interface & Method |
Type | Example |
---|---|---|
Supplier<E> E get() |
() -> E |
Supplier<List> factory = ArrayList::new; stream.collect(Collectors.toList(factory)); |
Consumer<E> void accept(E e) |
E -> () |
list.foreach(e -> e.frobnicate()) |
Runnable void run() |
() -> () |
new Thread(() -> System.sleep(10000)).run(); |
Function<T,R> R apply(T t) |
T -> R |
stream.map(t -> new R(t)); |
BiFunction<T,U,R> R apply(T t, U u) |
(T, U) -> R |
stream.reduce(start, accumulator, merge ) |
Predicate<E> boolean test(E e) |
E -> boolean |
Function<E, Boolean> but with primitive boolean |
When to use
Use | When | Related concept |
---|---|---|
Supplier<E> |
If it takes nothing | Factory |
Consumer<E> |
If it returns nothing | Listener, Callback |
Runnable |
If it does neither | Task |
Function<F, T> |
If it does both | Callback |
BiFunction<T, U , R> |
If it takes two and returns one | |
Predicate<E> |
To check semantic properties | Condition |
MapReduce
Important stream operations
As always, A = B
is permissible!
Operation | Type | Input | Output | See also |
---|---|---|---|---|
map |
A -> B |
Stream<A> |
Stream<B> |
flatMap |
filter |
A -> bool |
Stream<A> |
Stream<A> |
takeWhile |
reduce |
(B, ((B, A) -> B), ((B, B) -> B)) -> B |
Stream<A> |
B |
collect is reduce |
When to use
Use | When |
---|---|
map |
transforming one Stream into another Stream |
filter |
excluding elements from one stream based on a condition (predicate) |
reduce |
making the stream “smaller” (reducing it), e.g. - collecting multiple elements into one list - taking a sum, maximum or minimum - or otherwise reducing multiple elements to one element |