Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Java Extension Methods


Published on

A short introduction to public defender methods and project lambda

Published in: Technology
  • Be the first to comment

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 /><br />