Path: csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!news.netfront.net!not-for-mail From: Wanja Gayk Newsgroups: comp.lang.java.programmer Subject: Re: are lambdas a way to bring functional programming to java? Date: Sat, 28 Jun 2014 14:43:59 +0200 Organization: Netfront http://www.netfront.net/ Lines: 54 Message-ID: References: <53ae08d5$0$304$14726298@news.sunsite.dk> <53ae0af4$0$302$14726298@news.sunsite.dk> NNTP-Posting-Host: 77.176.52.252 Mime-Version: 1.0 Content-Type: text/plain; charset="iso-8859-15" Content-Transfer-Encoding: 8bit X-Trace: adenine.netfront.net 1403959408 86078 77.176.52.252 (28 Jun 2014 12:43:28 GMT) X-Complaints-To: news@netfront.net NNTP-Posting-Date: Sat, 28 Jun 2014 12:43:28 +0000 (UTC) User-Agent: MicroPlanet-Gravity/3.0.4 Xref: csiph.com comp.lang.java.programmer:28863 In article <53ae0af4$0$302$14726298@news.sunsite.dk>, Arne Vajhøj (arne@vajhoej.dk) says... > Here is a simple example. > > Java 7 code: > > Collections.sort(lst, new Comparator() { > public int compare(String s1, String s2) { > return s1.length() - s2.length(); > } > }); > > Java 8 code: > > Collections.sort(lst, (s1,s2) -> s1.length() - s2.length()); > > I do not see any value in the extra code in the 7 version. I fear Java8's Lambdas may not encourage reuse so much as the Java 7 way. This does seem to be a pretty standard comparator, so I would probably code it this way: static final Comparator COMPARE_BY_LENGTH = new Comparator<>{ public int compare(String s1, String s2) { return s1.length() - s2.length(); } }); Collections.sort(lst, COMPARE_BY_LENGTH); Just because using it looks better. Of course in Java 8 it could look like this (sorry for the bad line breaks: static final Comparator COMPARE_BY_LENGTH = (s1,s2) -> s1.length() - s2.length() Collections.sort(lst, COMPARE_BY_LENGTH); ..or even implementing a more general interface. But would people do that very often, if the original expression is so concise? Hmm.. Kind regards, Wanja -- ..Alesi's problem was that the back of the car was jumping up and down dangerously - and I can assure you from having been teammate to Jean Alesi and knowing what kind of cars that he can pull up with, when Jean Alesi says that a car is dangerous - it is. [Jonathan Palmer] --- news://freenews.netfront.net/ - complaints: news@netfront.net ---