Your SlideShare is downloading. ×
0
Java Extension Methods
Java Extension Methods
Java Extension Methods
Java Extension Methods
Java Extension Methods
Java Extension Methods
Java Extension Methods
Java Extension Methods
Java Extension Methods
Java Extension Methods
Java Extension Methods
Java Extension Methods
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 Extension Methods

3,092

Published on

A short introduction to public defender methods and project lambda

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,092
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
0
Comments
0
Likes
1
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. Extension Methods and Lambdas in Java 8<br />2011-03-03<br />Andreas Enbohm<br />
  • 2. 3 mars 2011<br />Sida 2<br />Extension Methods<br />Whathappenshere?<br />people.sort(#Person.lastName)<br />
  • 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. 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. 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 -&gt; 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. 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 -&gt; p.getLastName() }); <br />
  • 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 -&gt; p.getLastName() });<br />
  • 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. Extension Methods<br />and with Extension Methodswefinally get…<br />3 mars 2011<br />Sida 9<br />//Nice, wehavepeople (a List&lt;T&gt;) whichcandosorting. But //wait a minuteList&lt;T&gt; don’thave a sort(…) method… Howdid //this work?<br />people.sortBy( #Person.getLastName);<br />
  • 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. Enhancedlibraries<br />Proposed Syntax<br />3 mars 2011<br />Sida 11<br />public interface List&lt;T&gt; extends Collection&lt;T&gt; {<br /> public intsize();<br /> // The rest of the existing List methods<br /> extensionvoid sort(List&lt;T&gt; list) default Collections.&lt;T&gt;sort;<br />}<br />
  • 12. 3 mars 2011<br />Sida 12<br />Extension Methods<br />Questions?<br />people.eat(#Senadin.cake)<br />

×