Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Java 8 Hipster slides

Java 8 Hipster slides

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

Avatar for Oleg Prophet

Oleg Prophet

June 23, 2014
Tweet

Other Decks in Programming

Transcript

  1. JAVA 8 HIPSTER SLIDES NEW JAVA VERSION FROM FUNCTIONAL PROGRAMMER

    PERSPECTIVE Created by / Oleg Prophet @oregu_desu
  2. FEATURES Mixins, aka default methods Collection goodies More type inference

    Project Lambda Streams No Permgen. No OOME: permgen space errors*
  3. DEFAULT METHODS Known as Defender Methods Implementation methods in interfaces

    Poor man's Mixins Multiple inheritance (With ambiguity resolving mechanism!) Reduce abstract classes Utility methods “Adding a method to an interface is not now a source- compatible change”
  4. DEFAULT METHODS EXAMPLE p u b l i c i

    n t e r f a c e S i z e d { d e f a u l t b o o l e a n i s E m p t y ( ) { r e t u r n s i z e ( ) = = 0 ; } i n t s i z e ( ) ; } À-LA MIXINS EXAMPLE c l a s s V e r y F a s t C a r e x t e n d s A C a r i m p l e m e n t s I F a s t C a r , I F a s t S t e e r C a r { } c l a s s V e r y S l o w C a r e x t e n d s A C a r i m p l e m e n t s I S l o w C a r , I S l o w S t e e r C a r { } / / E v e n b e t t e r w o u l d b e ( y o u c a n i n S c a l a ) I C a r c a r = n e w A C a r ( ) w i t h I S l o w C a r , I F a s t S t e e r C a r ;
  5. MORE POWER TO INTERFACES Finally! Define static methods right in

    the interfaces. How that makes you feel, huh? Remove your Collections, Arrays, Paths now.
  6. COLLECTION GOODIES MAPS: getOrDefault(K, V) \m/ putIfAbsent(K, V) replace(K, V

    new) replace(K, V old, V new) compute(K, BiFunction) * computeIfAbsent(K, Function) * computeIfPresent(K, BiFunction) * merge(T, V, BiFunction) * Reduce your boilerplate.
  7. COLLECTION GOODIES Set and List didn't change interface much, but

    let's lookup Collection and Iterable. spliterator() * removeIf(Predicate) * stream() * parallelStream() * (Iterable).forEach(Consumer) * * We'll get to them in a moment.
  8. DATE/TIME GOODIES Since mutability is evil, we replaced java.util.Date class

    with a bunch of immutable java.time.* classes! “All the classes are immutable and thread-safe.”
  9. TYPE INFERENCE JAVA 7 v o i d p r

    o c e s s S t r i n g L s t ( L i s t < S t r i n g > l ) { . . . } L s t . p r o c e s s S t r i n g L s t ( L i s t . < S t r i n g > e m p t y ( ) ) ; JAVA 8 L s t . p r o c e s s S t r i n g L s t ( L i s t . e m p t y ( ) ) ; BUT STILL S t r i n g s = L s t . < S t r i n g > s i n g l e t o n ( ) . h e a d ( ) ; Meh…
  10. () → ​ {} Project Lambda (JSR #335) Initiated in

    December 2009 as Straw-Man proposal Loooong awaited Full class support Not a syntactic sugar for an anonymous inner class Even though it can appear so. They are not even objects.
  11. WITHOUT () → ​ {} L i s t <

    S t r i n g > n a m e s = n e w A r r a y L i s t < S t r i n g > ( ) ; f o r ( i n t i = 0 ; i < f i e l d s . s i z e ( ) ; i + + ) { F i e l d f l d = f i e l d s . g e t ( i ) ; n a m e s . a d d ( f l d . g e t N a m e ( ) ) ; } f o r ( i n t i = 0 ; i < n a m e s . s i z e ( ) ; i + + ) { S t r i n g n a m e = n a m e s . g e t ( i ) ; S y s t e m . o u t . p r i n t l n ( n a m e ) ; }
  12. () → ​ {} n a m e s =

    f i e l d s . s t r e a m ( ) . m a p ( F i e l d : : g e t N a m e ) . c o l l e c t ( t o L i s t ( ) ) ; n a m e s . f o r E a c h ( S y s t e m . o u t : : p r i n t l n ) ;
  13. () → ​ {} n a m e s .

    m a p ( ( S t r i n g s ) - > { r e t u r n s . l e n g t h ( ) ; } ) ; We know it's a collection of strings! n a m e s . m a p ( ( s ) - > s . l e n g t h ( ) ) ; That's not a LISP! Who likes parentheses anyway? n a m e s . m a p ( s - > s . l e n g t h ( ) ) ; Can I have a method reference, please? n a m e s . m a p ( S t r i n g : : l e n g t h ) ; Thank you, Java 8.
  14. MORE () → {} EXAMPLES / / G r o

    u p e m p l o y e e s b y d e p a r t m e n t M a p < D e p a r t m e n t , L i s t < E m p l o y e e > > b y D e p t = e m p l o y e e s . s t r e a m ( ) . c o l l e c t ( g r o u p i n g B y ( E m p l o y e e : : g e t D e p a r t m e n t ) ) ; / / P a r t i t i o n s t u d e n t s i n t o p a s s i n g a n d f a i l i n g M a p < B o o l e a n , L i s t < S t u d e n t > > p a s s i n g F a i l i n g = s t u d e n t s . s t r e a m ( ) . c o l l e c t ( p a r t i t i o n i n g B y ( s - > s . g e t G r a d e ( ) > = P A S S _ T H R E S H O L D ) ) ; / / C l a s s i f y p e o p l e b y s t a t e a n d c i t y M a p < S t r i n g , M a p < S t r i n g , L i s t < P e r s o n > > > p e o p l e B y S t a t e A n d C i t y = p e r s o n S t r e a m . c o l l e c t ( g r o u p i n g B y ( P e r s o n : : g e t S t a t e , g r o u p i n g B y ( P e r s o n : : g e t C i t y ) ) )
  15. FUNCTIONAL INTERFACES @ F u n c t i o

    n a l I n t e r f a c e p u b l i c i n t e r f a c e F u n c t i o n < T , R > { R a p p l y ( T t ) ; } F u n c t i o n < S t r i n g , S t r i n g > m = s - > s . t o U p p e r C a s e ( ) ; F u n c t i o n < S t r i n g , I n t e g e r > f = S t r i n g : : l e n g t h ; F u n c t i o n g = f . a n d T h e n ( I n t e g e r : : r e v e r s e ) ; F u n c t i o n i d = F u n c t i o n . i d e n t i t y ( ) ;
  16. COMPOSE LIKE A PRO Function composition f : X → Y

    g : Y → Z g ∘ f : X → Z F u n c t i o n < S t r i n g , I n t e g e r > f = S t r i n g : : l e n g t h ; F u n c t i o n < I n t e g e r , F l o a t > g = I n t e g e r : : f l o a t V a l u e ; F u n c t i o n h = g . c o m p o s e ( f ) ;
  17. CURRY LIKE A PRO F u n c t i

    o n < S t r i n g , U n a r y O p e r a t o r < S t r i n g > > c u r r i e d = s 1 - > s 2 - > s 1 . c o n c a t ( " " ) . c o n c a t ( s 2 ) ; / / P a r t i a l a p p l i c a t i o n U n a r y O p e r a t o r < S t r i n g > h a s k = c u r r i e d . a p p l y ( " H a s k e l l " ) ; o u t . p r i n t l n ( h a s k . a p p l y ( " C u r r y " ) ) ; o u t . p r i n t l n ( h a s k . a p p l y ( " W e x l e r " ) ) ; * Currying is a fancy name for schönfinkeling
  18. CURRY LIKE A SEMI-PRO Can't curry any function like (a,

    b) → a + b; But we have tools: p u b l i c i n t e r f a c e C u r r y { s t a t i c < T , U , R > F u n c t i o n < U , R > c u r r y ( B i F u n c t i o n < T , U , R > b i , T t ) { r e t u r n u - > b i . a p p l y ( t , u ) ; } } B i F u n c t i o n < S t r i n g , I n t e g e r , F l o a t > b i = ( s , i ) - > ( s . l e n g t h ( ) + i ) / 2 . 0 f ; / / C a n ' t d o b i . c u r r y ( " h e l l o " ) f o r a n y b i F u n c t i o n < I n t e g e r , F l o a t > p a r t = C u r r y . c u r r y ( b i , " h e l l o " ) ; / / W i l l w e b e a b l e c a l l p a r t ( 1 0 ) s o m e d a y ? o u t . p r i n t l n ( p a r t . a p p l y ( 1 0 ) ) ; o u t . p r i n t l n ( p a r t . a p p l y ( 2 2 ) ) ;
  19. STREAMS forEach * forEachOrdered toArray reduce collect * min, max,

    count, sum (any|all|none)Match findAny * These are terminal operations They are not lazy at all. No element will be produced until you call one of these. * Collectors api: toList(), counting(), joining(), etc.
  20. PARALLEL STREAMS From sequential to parallel in the blink of

    an eye l n s = n a m e s . p a r a l l e l S t r e a m ( ) . c o l l e c t ( g r o u p i n g B y ( S t r i n g : : l e n g t h ) ) ; l n s . f o r E a c h ( ( k e y , n s ) - > o u t . p r i n t l n ( k e y + " : \ t " + n s . s t r e a m ( ) . c o l l e c t ( j o i n i n g ( " , " ) ) ) ) ;
  21. FAILED COMPUTATION? f i n d A n y (

    ) returns special container object O p t i o n a l i s P r e s e n t , i f P r e s e n t ( C o n s u m e r ) o r E l s e , o r E l s e ( S u p p l i e r ) , o r E l s e T h r o w Treat like collection: m a p , f l a t M a p , f i l t e r Create Optional: e m p t y , o f ( v a l ) , o f N u l l a b l e ( v a l ) A convenient way to represent result absence. (And reduce NPE count.)
  22. NO MORE PERMGEN No more PermGen space errors and PermGen

    tuning. Java says: VM warning: ignoring option MaxPermSize=512m; support was removed in 8.0 Jon Masamitsu: A goal for removing perm gen was so that users do not have to think about correctly sizing it. — But where are my class instances?
  23. METASPACE Native memory region for class data. Grow automatically by

    default. Garbage collected. -XX:MetaspaceSize -XX:MaxMetaspaceSize Transition to Java 8: e / P e r m / M e t a s p a c e / g
  24. BUT, OLEG, WAIT! — You said this is Hipster slides,

    but you didn't even mention a monad! — Sorry guys. No monads until we'll have Higher Kinded Polymorphism in Java!