Your SlideShare is downloading. ×
0
Java 8 Hipster slides
Java 8 Hipster slides
Java 8 Hipster slides
Java 8 Hipster slides
Java 8 Hipster slides
Java 8 Hipster slides
Java 8 Hipster slides
Java 8 Hipster slides
Java 8 Hipster slides
Java 8 Hipster slides
Java 8 Hipster slides
Java 8 Hipster slides
Java 8 Hipster slides
Java 8 Hipster slides
Java 8 Hipster slides
Java 8 Hipster slides
Java 8 Hipster slides
Java 8 Hipster slides
Java 8 Hipster slides
Java 8 Hipster slides
Java 8 Hipster slides
Java 8 Hipster slides
Java 8 Hipster slides
Java 8 Hipster slides
Java 8 Hipster slides
Java 8 Hipster slides
Java 8 Hipster slides
Java 8 Hipster slides
Java 8 Hipster slides
Java 8 Hipster slides
Java 8 Hipster slides
Java 8 Hipster slides
Java 8 Hipster slides
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

Java 8 Hipster slides

965

Published on

New Java version from the functional programmer perspective. …

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

Published in: Technology, News & Politics
0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
965
On Slideshare
0
From Embeds
0
Number of Embeds
4
Actions
Shares
0
Downloads
14
Comments
0
Likes
2
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. JAVA 8 HIPSTER SLIDES NEW JAVA VERSION FROM FUNCTIONAL PROGRAMMER PERSPECTIVE Createdby /OlegProphet @oregu_desu
  • 2. JAVA 8 03/18/2014 Lastupdate: 5 Unofficialtagline: YOU CAN BE HIPSTER TOO.
  • 3. FEATURES Mixins, akadefaultmethods Collection goodies More type inference ProjectLambda Streams No Permgen. No OOME: permgen space errors*
  • 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”
  • 5. DEFAULT METHODS EXAMPLE publicinterfaceSized{ defaultbooleanisEmpty(){ returnsize()==0; } intsize(); } À-LA MIXINS EXAMPLE classVeryFastCarextendsACarimplementsIFastCar,IFastSteerCar{} classVerySlowCarextendsACarimplementsISlowCar,ISlowSteerCar{} //Evenbetterwouldbe(youcaninScala) ICarcar=newACar()withISlowCar,IFastSteerCar;
  • 6. MORE POWER TO INTERFACES Finally!Define static methods right in the interfaces. How thatmakes you feel, huh? Remove your Collections, Arrays, Paths now.
  • 7. COLLECTION GOODIES MAPS: getOrDefault(K, V) m/ putIfAbsent(K, V) replace(K, Vnew) replace(K, Vold, Vnew) compute(K, BiFunction) * computeIfAbsent(K, Function) * computeIfPresent(K, BiFunction) * merge(T, V, BiFunction) * Reduce your boilerplate.
  • 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.”
  • 10. TYPE INFERENCE JAVA 7 voidprocessStringLst(List<String>l){...} Lst.processStringLst(List.<String>empty()); JAVA 8 Lst.processStringLst(List.empty()); BUT STILL Strings=Lst.<String>singleton().head(); Meh…
  • 11. TYPE INFERENCE More we'llsee in lambdaslides
  • 12. LAMBDA SLIDES () → ​{}
  • 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.
  • 14. WITHOUT () → ​{} List<String>names=newArrayList<String>(); for(inti=0;i<fields.size();i++){ Fieldfld=fields.get(i); names.add(fld.getName()); } for(inti=0;i<names.size();i++){ Stringname=names.get(i); System.out.println(name); }
  • 15. () → ​{} names=fields.stream().map(Field::getName).collect(toList()); names.forEach(System.out::println);
  • 16. () → ​{} names.map((Strings)->{returns.length();}); We know it's acollection of strings! names.map((s)->s.length()); That's notaLISP!Who likes parentheses anyway? names.map(s->s.length()); Can I have amethod reference, please? names.map(String::length); Thank you, Java8.
  • 17. () → ​{} METHOD REFERENCES Object::toString Field::create Field::new this::processField a::process (ais some objectin scope)
  • 18. MORE () → {} EXAMPLES //Groupemployeesbydepartment Map<Department,List<Employee>>byDept =employees.stream().collect(groupingBy(Employee::getDepartment)); //Partitionstudentsintopassingandfailing Map<Boolean,List<Student>>passingFailing =students.stream().collect(partitioningBy( s->s.getGrade()>=PASS_THRESHOLD)); //Classifypeoplebystateandcity Map<String,Map<String,List<Person>>>peopleByStateAndCity =personStream.collect(groupingBy(Person::getState, groupingBy(Person::getCity)))
  • 19. FUNCTIONAL INTERFACES @FunctionalInterface publicinterfaceFunction<T,R>{ Rapply(Tt); } Function<String,String>m=s->s.toUpperCase(); Function<String,Integer>f=String::length; Functiong=f.andThen(Integer::reverse); Functionid=Function.identity();
  • 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));
  • 23. JAVA.UTIL.FUNCTION.* Function<T, R> BiFunction<T, U, R> Predicate<T> Supplier<T> Consumer<T> BiConsumer<T, U> UnaryOperator<T> : Function<T, T>
  • 24. STREAMS filter map flatMap distinct sorted limit These are intermediate operations Theyare alllazy.
  • 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(","))));
  • 27. FAILED COMPUTATION? findAny()returns specialcontainer objectOptional isPresent, ifPresent(Consumer) orElse, orElse(Supplier), orElseThrow Treatlike collection: map, flatMap, filter Create Optional: empty, of(val), ofNullable(val) Aconvenientwayto representresultabsence. (And reduce NPE count.)
  • 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?
  • 29. METASPACE!
  • 30. METASPACE! java.lang.OutOfMemoryError: Metadataspace
  • 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!
  • 33. Source: , THE END BY OLEG PROPHET / HAKUTAKU.ME slides javasamples Thank you!

×