Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!newsfeed1.swip.net!goblin3!goblin.stu.neva.ru!news.netfront.net!not-for-mail From: Lothar Kimmeringer Newsgroups: comp.lang.java.help Subject: Re: Lambdas/Callback Functions/Functional Programming. What is the point? Date: Fri, 23 Jan 2015 22:17:18 +0100 Organization: Organization?! Only chaos here! Lines: 65 Message-ID: References: Reply-To: news@kimmeringer.de NNTP-Posting-Host: 93.133.93.82 Mime-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit X-Trace: adenine.netfront.net 1422047837 10688 93.133.93.82 (23 Jan 2015 21:17:17 GMT) X-Complaints-To: news@netfront.net NNTP-Posting-Date: Fri, 23 Jan 2015 21:17:17 +0000 (UTC) User-Agent: 40tude_Dialog/2.0.15.1de Xref: csiph.com comp.lang.java.help:3152 Steve wrote: > I want to understand why Oracle implemented lambdas, > streams, and functional programming. One reason was, that everybody asked for this ;-) > Basically, what I would like to understand is what are the > benefits of these things? One reason, why I waited so long for this is Delayed Initialization. Resources are only created when they are necessary so instead of all the repeated code like this: public class HeavyResource { private ResourceData resData = null; public String getResourceResult() { if (resData == null){ getResourceData(); } return resData.getResult(); } } you can now write public class HeavyResouerce { private ResourceData resData = () -> getResourceData(); public String getResourceResult() { return resData.getResult(); } } The automatic creation if the resource hasn't been created is done the moment the first access takes place to it. And it is done in a thread-safe way (that many self made solutions often fail to implement correctly) Another feature is Lazy Evaluation. If you call a method evaluate(heavyCalc1(), heavyCalc(2)) both heavyCalc-methods are called before the call of evaluate. If the result of heavyCalc1 is enough for the evaluation the call of heavyCalc2 was useless. Simple real-world-example: logDebug(callStatusWebService()); With Lazy Evaluation the methods in the parameters are only called if they are actually needed in the method. A third feature is the ability to parallelize streams. Just call .parallelStream instead of .stream and your whole operation will be performed on all available CPUs instead of only one. Best regards, Lothar -- Lothar Kimmeringer E-Mail: spamfang@kimmeringer.de PGP-encrypted mails preferred (Key-ID: 0x8BC3CD81) Always remember: The answer is forty-two, there can only be wrong questions! --- news://freenews.netfront.net/ - complaints: news@netfront.net ---