BafS - Java8-CheatSheet - A Java 8+ Cheat Sheet For Functional Programming
BafS - Java8-CheatSheet - A Java 8+ Cheat Sheet For Functional Programming
BafS / Java8-CheatSheet
Branch: master New pull request Create new file Upload files Find file Clone or download
BafS Merge pull request #4 from Gulivert/master … Latest commit 958fa25 on Feb 16, 2018
README.md Add note on inferance limitations (thanks to @galedric #2) 4 years ago
README.md
Lambda Expression
If the lambda is more than one expression we can use { } and return
(x, y) -> {
int sum = x + y;
int avg = sum / 2;
return avg;
}
A lambda expression cannot stand alone in Java, it need to be associated to a functional interface.
interface MyMath {
int getDoubleOf(int a);
}
Collections
sort sort(list, comparator)
https://github.com/BafS/Java8-CheatSheet 1/5
2/27/2020 BafS/Java8-CheatSheet: A Java 8+ Cheat Sheet for functional programming
removeIf
// Lambda Form:
getPrimes(numbers, a -> StaticMethod.isPrime(a));
// Method Reference:
getPrimes(numbers, StaticMethod::isPrime);
Streams
Similar to collections, but
https://github.com/BafS/Java8-CheatSheet 2/5
2/27/2020 BafS/Java8-CheatSheet: A Java 8+ Cheat Sheet for functional programming
Collecting results
map map(mapper)
Applying a function to each element
filter filter(predicate)
Retains elements that match the predicate
reduce
Reduce the elements to a single value
res = stream.limit(3);
//> Bohr Darwin Galilei
res = Stream.of(1,0,0,1,0,1).distinct();
//> 1 0
https://github.com/BafS/Java8-CheatSheet 3/5
2/27/2020 BafS/Java8-CheatSheet: A Java 8+ Cheat Sheet for functional programming
res = stream.sorted();
//> Bohr Darwin Einstein Galilei Newton Tesla
allMatch
Primitive-Type Streams
Wrappers (like Stream) are inefficients. It requires a lot of unboxing and boxing for each element. Better to use IntStream ,
DoubleStream , etc.
Creation
Use mapToX (mapToObj, mapToDouble, etc.) if the function yields Object, double, etc. values.
Grouping Results
Collectors.groupingBy
// Groupe by length
Map<Integer, List<String>> groups = stream
.collect(Collectors.groupingBy(w -> w.length()));
//> 4=[Bohr], 5=[Tesla], 6=[Darwin, Newton], ...
Collectors.toSet
PS: Don't forget Optional (like Map<T, Optional<T>> ) with some Collection methods (like Collectors.maxBy ).
Parallel Streams
Creation
https://github.com/BafS/Java8-CheatSheet 4/5
2/27/2020 BafS/Java8-CheatSheet: A Java 8+ Cheat Sheet for functional programming
stream.parallelStream().unordered().distinct();
PS: Work with the streams library. Eg. use filter(x -> x.length() < 9) instead of a forEach with an if .
Optional
In Java, it is common to use null to denote absence of result. Problems when no checks: NullPointerException .
Return an Optional
Optional<Double> squareRoot(double x) {
if (x >= 0) { return Optional.of(Math.sqrt(x)); }
else { return Optional.empty(); }
}
stream.sorted(Comparator.comparing(Pair::first)) // ok
Java cannot infer type for the .comparing(Pair::first) part and fallback to Object, on which Pair::first cannot be
applied.
The required type for the whole expression cannot be propagated through the method call ( .thenComparing ) and used to
infer type of the first part.
stream.sorted(
Comparator.<Pair<String, Long>, String>comparing(Pair::first)
.thenComparing(Pair::second)
) // ok
This cheat sheet was based on the lecture of Cay Horstmann http://horstmann.com/heig-vd/spring2015/poo/
https://github.com/BafS/Java8-CheatSheet 5/5