You are on page 1of 2

findAny, T Optional<T

findFirst >
Java 8 features cheat sheet forEach T Void Consumer<T>
collect T R Collector<T,A,R>
reduce T Optional<T BinaryOperator<T
Lambdas > >
Based on @FunctionalInterface count T Long
iterate I Void Stream<T>
(parameters) -> expression generate I Void Stream<T>
(parameters) -> {statements;}
Examples: () -> {} | ()->”Henry” Examples:
(Car c)-> { return Car::Engine;} (x)->x*x transactions.stream().anyMatch(transaction-
Predicate<T> T -> Boolean >transaction.getTrader().getCity().equals(“LA”);
Consumer<T> T-> void tr.stream().map(Transaction::getValue).reduce(I
Supplier<T> () ->T
nteger::sum)=tr.stream().toIntMap(Transaction:
Function<T,R> T-> R
UnaryOperator<T> T->T :getValue).sum();
BinaryOperator<T> (T,T) -> T tr.stream().map(Transaction::getTrader).filter(tr
BiPredicate<L,R> (L,R) -> boolean
ader-
BiConsumer<L,R> (L,R) -> void
BiFunction<L,R,U> (L,R)->U >trader.getCity().equals(“LA”).distinct().sorted(c
omparing(Trader::getName)).collect(toList());
menu.stream().map(Dish::getName).collect(join
Method Reference ing(“, “));
1. Static method Integer::parseInt menu.stream().collect(groupingBy(Dish::getTyp
2. Instance method String::length e,groupingBy(Dish::getSpycines));
3. Existing object car::getEngine menu.stream().partitionBy(Dish::isVegeterian,gr
Example oupBy(Dish::getType));
stringList.sort(String::compareToIgnoreCase) Collectors:
Supplier<Car> c1 = Car::new; Car c = c1.get()
Function<Int..,Car> c2=Car::new; c=c2.apply(10) toList, toSet, toCollection
(toCollection(),ArrayList::new)
Comparator.comparing(Car::getWeight).reverse counting,summingInt,averagingInt,summurizing
d().thenComparing(Car::getModel) Int(summingInt(Dish::getCalories())
joining (joining(“, “))
maxBy,minBy(collect(minBy(comparingInt(Dish:
Streams :getCalories)))
Stream is a set of values spread out in time, reducing
collection is set of values spread out in space (reducing(0,Dish::getCalories,Integer::sum)
filter I Stream<T> Predicate<T> collectingAndThen,groupingBy,partitioningBy
distinct I Stream<T>
skip I Stream<T> long Default methods
limit I Stream<T> Long
Needed to evolve API
map I Stream<R> Function<T,R>
flatMap I Stream<R> Function<T,
Stream<R>>
Example:
sorted I Stream<T> Comparator<T> default List<T> sort(List<T> l){
anyMatch, T Bool Predicate<T> Collections.sort(l);}
noneMatch
, allMatch,
Optional
Handle null values better, declare that method Examples:
could return a null value. shops.stream().map(shop ->
Optional.empty(); Optional.of(o); CompletableFuture.supplyAsync(() ->
Optional.ofNullabe(o) shop.getPrice(product),executor)).map(future -
> future.thenApply(Quote::parse)).map(future -
Examples: > future.thenCompose(quote->
optoinalP.flatMap(Person:getCar).flatMap(Car:: CompletableFuture.sypplyAsync(() ->
getEngine).map(Engine::getCylinders).orElse(val Discount.applyDiscount(quoute), executor)) );
ue);

optionalC.ifPresnet(); optionalC.get();
optionalC.orElseThrow(e) Date and Time API
Main purpose is to overcome the limitation of
Completable Future existing date APIs
Make it easy to work with futures and parallel
computations. Classes (inspired by JIDE package):

Ntrheads=Ncpu*Ucpu(1+W/C) where Ncpu is number LocalDate, LocalTime, LocalDateTime, Instant,


of core CPU Duration, Period.
(Runtime.getRuntime().availableProcessors()) Date manipulations are done by
Ucpu is target CPU utilization between 0 and 1 TemporalAdjuster functional interface.
and W/C is ration of wait to compute time. If
there is I/O and few compute statements ratio
is 100 since wait is dominant.
supplyAsync Asynchronous task to
get executed. You can
pass executor as a
second parameter
thenApply Dependent computation
to be performed on the
same thread as prior
computation
thenCompose Dependent computation
to be applied on new
thread
thenCombine Merging current parallel
computation with
another parallel
computation define as a
second parameter.
Third parameter
describes how to
combine the
computations
thenAccept Take a consumer and
applies it to the
computed result
performed on the same
thread.
thenAcceptAsync Same as thenAccept
done asynchronously

You might also like