Extension Methods and Lambdas in Java 8<br />2011-03-03<br />Andreas Enbohm<br />
3 mars 2011<br />Sida 2<br />Extension Methods<br />Whathappenshere?<br />people.sort(#Person.lastName)<br />
Extension Methods<br />Whatwehave in Java today…<br />Toomuchnoise!<br />Collections.sort(people, new Comparator() {<br />...
SAM types and Lambdas<br />Single Abstract Method (SAM)<br />3 mars 2011<br />Sida 4<br />Thread th = new Thread(new Runna...
3 mars 2011<br />Sida 5<br />SAM and Lambdas conversion<br />With Lambdas and SAM it looks like<br />Collections.sort(peop...
EnchancedLibraries (Collections)<br />But it gets evenbetter with EnchancedLibraries<br />3 mars 2011<br />Sida 6<br />//C...
TypeInference<br />…and with somehelp from the compiler…we get TypeInference<br />3 mars 2011<br />Sida 7<br />//No need t...
MethodReference<br />A new feature with Project Lambdas is MethodReferenceswhich gives us…<br />3 mars 2011<br />Sida 8<br...
Extension Methods<br />and with Extension Methodswefinally get…<br />3 mars 2011<br />Sida 9<br />//Nice, wehavepeople (a ...
Extension Methods (DefenderMethods)<br />A way to evolve interfaces WITHOUT breakingbinarybackwardscompatability.<br />Mea...
Enhancedlibraries<br />Proposed Syntax<br />3 mars 2011<br />Sida 11<br />public interface List<T> extends Collection<T> {...
3 mars 2011<br />Sida 12<br />Extension Methods<br />Questions?<br />people.eat(#Senadin.cake)<br />
Upcoming SlideShare
Loading in...5
×

Java Extension Methods

3,128

Published on

A short introduction to public defender methods and project lambda

Published in: Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
3,128
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
0
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Transcript of "Java Extension Methods"

  1. 1. Extension Methods and Lambdas in Java 8<br />2011-03-03<br />Andreas Enbohm<br />
  2. 2. 3 mars 2011<br />Sida 2<br />Extension Methods<br />Whathappenshere?<br />people.sort(#Person.lastName)<br />
  3. 3. Extension Methods<br />Whatwehave in Java today…<br />Toomuchnoise!<br />Collections.sort(people, new Comparator() {<br /> @Override<br /> public intcompare(Person x, Person y) {<br />returnx.getLastName().compareTo(y.getLastName());<br /> }<br />});<br />3 mars 2011<br />Sida 3<br />
  4. 4. SAM types and Lambdas<br />Single Abstract Method (SAM)<br />3 mars 2011<br />Sida 4<br />Thread th = new Thread(new Runnable() { <br /> public void run() { <br />doSomeStuff(); <br />doMoreStuff();<br /> } }); //OLD WAY<br />Thread th = new Thread(#(){ <br />doSomeStuff(); <br />doMoreStuff(); } ) <br />
  5. 5. 3 mars 2011<br />Sida 5<br />SAM and Lambdas conversion<br />With Lambdas and SAM it looks like<br />Collections.sort(people, #{ Person x, Person y -> x.getLastName().compareTo(y.getLastName()) });<br />//Old way, let’shopewesooncan @Deprecate this code<br />Collections.sort(people, new Comparator() {<br /> @Override<br /> public intcompare(Person x, Person y) {<br />returnx.getLastName().compareTo(y.getLastName());<br /> }<br />});<br />
  6. 6. EnchancedLibraries (Collections)<br />But it gets evenbetter with EnchancedLibraries<br />3 mars 2011<br />Sida 6<br />//Collections is updated. No need to specify Person x and <br />//Person y, just say ’take a person in the [list] and sort by<br />//last name’ (its implicit that compareToshould be used)<br />Collections.sortBy(people, #{ Person p -> p.getLastName() }); <br />
  7. 7. TypeInference<br />…and with somehelp from the compiler…we get TypeInference<br />3 mars 2011<br />Sida 7<br />//No need to specify Person, the compilercan (often) helpus //with this. Compilerintrospectpeople-list via generics<br />Collections.sortBy(people, #{ p -> p.getLastName() });<br />
  8. 8. MethodReference<br />A new feature with Project Lambdas is MethodReferenceswhich gives us…<br />3 mars 2011<br />Sida 8<br />//Methodreferenceallowsus to ’point’ directly to the method<br />//wewant to use in oursorting. Whyuse a particularobject?<br />Collections.sortBy(people, #Person.getLastName );<br />
  9. 9. Extension Methods<br />and with Extension Methodswefinally get…<br />3 mars 2011<br />Sida 9<br />//Nice, wehavepeople (a List<T>) whichcandosorting. But //wait a minuteList<T> don’thave a sort(…) method… Howdid //this work?<br />people.sortBy( #Person.getLastName);<br />
  10. 10. Extension Methods (DefenderMethods)<br />A way to evolve interfaces WITHOUT breakingbinarybackwardscompatability.<br />Means that wecanadd new methods in an existing interface and the implementingclientsdoes NOT need to implement that method!<br />A reallyneed for updatingaging Collections interface, especiallywhen lambdas and Fork-Join are introduced in Java<br />The interface willsupply a default implementationif you do not supplyoneyourself<br />3 mars 2011<br />Sida 10<br />
  11. 11. Enhancedlibraries<br />Proposed Syntax<br />3 mars 2011<br />Sida 11<br />public interface List<T> extends Collection<T> {<br /> public intsize();<br /> // The rest of the existing List methods<br /> extensionvoid sort(List<T> list) default Collections.<T>sort;<br />}<br />
  12. 12. 3 mars 2011<br />Sida 12<br />Extension Methods<br />Questions?<br />people.eat(#Senadin.cake)<br />

×