New Java version from the functional programmer perspective.
Talk I gave @ Informatica.
Covers most major Java 8 additions including default methods, lambda expressions, PermGen removal, stream api, etc.
Slides online: http://hakutaku.me/j8
Code examples: https://github.com/Oregu/j8
4. DEFAULT METHODS
Known as Defender Methods
Implementation methods in interfaces
Poor man's Mixins
Multiple inheritance
(With ambiguityresolvingmechanism!)
Reduce abstractclasses
Utilitymethods
“Addingamethod to an interface is notnow asource-
compatible change”
6. MORE POWER TO INTERFACES
Finally!Define static methods right in the interfaces.
How thatmakes you feel, huh?
Remove your Collections, Arrays, Paths now.
8. COLLECTION GOODIES
Setand Listdidn'tchange interface much, butlet's lookup
Collection and Iterable.
spliterator() *
removeIf(Predicate) *
stream() *
parallelStream() *
(Iterable).forEach(Consumer) *
*We'llgetto them in amoment.
9. DATE/TIME GOODIES
Since mutabilityis evil, we replaced java.util.Date class with a
bunch of immutable java.time.*classes!
“Allthe classes are immutable and thread-safe.”
13. () → {}
ProjectLambda(JSR#335)
Initiated in December 2009 as Straw-Man proposal
Loooongawaited
Fullclass support
Notasyntactic sugar for an anonymous inner class
Even though itcan appear so.
Theyare noteven objects.
20. COMPOSE LIKE A PRO
Function composition
f : X → Y
g : Y → Z
g ∘ f : X → Z
Function<String,Integer>f=String::length;
Function<Integer,Float>g=Integer::floatValue;
Functionh=g.compose(f);
21. CURRY LIKE A PRO
Function<String,UnaryOperator<String>>curried=
s1->s2->s1.concat("").concat(s2);
//Partialapplication
UnaryOperator<String>hask=curried.apply("Haskell");
out.println(hask.apply("Curry"));
out.println(hask.apply("Wexler"));
*Curryingis afancyname for schönfinkeling
22. CURRY LIKE A SEMI-PRO
Can'tcurryanyfunction like (a, b) → a+ b;
Butwe have tools:
publicinterfaceCurry{
static<T,U,R>Function<U,R>curry(BiFunction<T,U,R>bi,Tt){
returnu->bi.apply(t,u);
}
}
BiFunction<String,Integer,Float>bi=(s,i)->(s.length()+i)/2.0f;
//Can'tdobi.curry("hello")foranybi
Function<Integer,Float>part=Curry.curry(bi,"hello");
//Willwebeablecallpart(10)someday?
out.println(part.apply(10));
out.println(part.apply(22));
25. STREAMS
forEach *
forEachOrdered
toArray
reduce
collect*
min, max, count, sum
(any|all|none)Match
findAny*
These are terminaloperations
Theyare notlazyatall.
No elementwillbe produced untilyou callone of these.
*Collectors api: toList(), counting(), joining(), etc.
26. PARALLEL STREAMS
From sequentialto parallelin the blink of an eye
lns=names.parallelStream().collect(groupingBy(String::length));
lns.forEach((key,ns)->out.println(key+":t"+
ns.stream().collect(joining(","))));
28. NO MORE PERMGEN
No more PermGen space errors and PermGen tuning.
Javasays:
VM warning: ignoringoption MaxPermSize=512m;
supportwas removed in 8.0
Jon Masamitsu:
Agoal for removing permgen was so thatusers
do nothave to thinkaboutcorrectly sizing it.
— Butwhere are myclass instances?
31. METASPACE
Native memoryregion for class data.
Grow automaticallybydefault.
Garbage collected.
-XX:MetaspaceSize -XX:MaxMetaspaceSize
Transition to Java8: e/Perm/Metaspace/g
32. BUT, OLEG, WAIT!
— You said this is Hipster slides, butyou didn'teven mention a
monad!
— Sorryguys. No monads untilwe'llhave Higher Kinded
Polymorphism in Java!