Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!eternal-september.org!feeder.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail From: markspace Newsgroups: comp.lang.java.programmer Subject: Re: are lambdas a way to bring functional programming to java? Date: Tue, 01 Jul 2014 14:34:02 -0700 Organization: A noiseless patient Spider Lines: 40 Message-ID: References: <53ae08d5$0$304$14726298@news.sunsite.dk> <53ae0af4$0$302$14726298@news.sunsite.dk> Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-15; format=flowed Content-Transfer-Encoding: 8bit Injection-Date: Tue, 1 Jul 2014 21:34:05 +0000 (UTC) Injection-Info: mx05.eternal-september.org; posting-host="5aa64ebf9a53573f97c75abba936b0e2"; logging-data="10098"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/HkTHm9JDvwils2vJlahA8+tSzXljSrl4=" User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:24.0) Gecko/20100101 Thunderbird/24.6.0 In-Reply-To: Cancel-Lock: sha1:csZhM4wsPYuI2apSCYnVqqfuWWg= Xref: csiph.com comp.lang.java.programmer:28893 On 6/28/2014 5:43 AM, Wanja Gayk wrote: > > static final Comparator COMPARE_BY_LENGTH > = (s1,s2) -> s1.length() - s2.length() > > Collections.sort(lst, COMPARE_BY_LENGTH); In that video you just posted featuring Brian Goetz talking about lambdas, Mr. Goetz says this is a bad idea. This costs a static field and the time it takes to initialize the field during startup. And you may never use that field, so you just spent time initializing unnecessarily. > In article <53ae0af4$0$302$14726298@news.sunsite.dk>, Arne Vajhøj >> Java 8 code: >> >> Collections.sort(lst, (s1,s2) -> s1.length() - s2.length()); >> Whereas this code above seems to be "better" because the runtime compiler can hoist the lambda out of the method call and cache it, just like you did manually, but the compiler can do it lazily at runtime, and won't do it at all if it's never needed. If you must have a named variable, I think this might be as good as the single line technique. { ... final Comparator byLength = (s1,s2) -> s1.length() - s2.length(); Collections.sort( lst, byLength ); } (Not compiled or tested.) 100% speculation by me, but a bit informed by just having watched Mr. Goetz on video.