Path: csiph.com!eternal-september.org!feeder.eternal-september.org!mx02.eternal-september.org!.POSTED!not-for-mail From: Eric Sosman Newsgroups: comp.lang.java.programmer Subject: Re: Lambda reference? Date: Mon, 14 Mar 2016 17:16:05 -0400 Organization: A noiseless patient Spider Lines: 59 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit Injection-Date: Mon, 14 Mar 2016 21:12:59 -0000 (UTC) Injection-Info: mx02.eternal-september.org; posting-host="2d081e2f7155c28005d4472f67c2f43d"; logging-data="9670"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18hdaKeDaYdg+yj5YQiut7A" User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:38.0) Gecko/20100101 Thunderbird/38.6.0 In-Reply-To: Cancel-Lock: sha1:hBKPaevtCqRuhqWfMIjtJjUBRhY= Xref: csiph.com comp.lang.java.programmer:33961 On 3/14/2016 4:32 PM, Rhino wrote: > I'm just new to Java Lambda expressions. I'm both excited and frustrated > by the Oracle tutorials describing them: the tutorial shows how powerful > they are and that's got me keen to know more but I'm not finding the > detailed references I need to be able to do what I want to do. > [...] See Sebastian's response. I'd just like to add that lambdas and streams are two different things, and that you may confuse yourself if you try to conflate them. Lambda expressions are often useful with streams, but you can use streams without them. Streams are frequent users of lambda expressions, but lambda expressions have non-stream uses as well. Here's a fragment using a lambda, with no stream (or collection) anywhere in sight: gadget.addActionListener((evt) -> { ... do something with the ActionEvent ... }); ... which we used to have to write as: gadget.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent evt) { ... do something with the ActionEvent ... } }); And here's a stream operation with no lambda in the neighborhood: Predicate pred = new Predicate<>() { @Override public boolean test(Thing thing) { ... long, complicated computation ... return answer; } }; List theChosenFew = allThings.stream() .filter(pred) .collect(Collectors.toList()); ... which we might have lambda-ized (if it's not too bulky): List theChosenFew = allThings.stream() .filter((thing) -> { ... long, complicated computation ... return answer; }) .collect(Collectors.toList()); So: Although they are frequently used together, try to bear in mind that there's no "necessary" connection between lambda expressions and stream operations. -- esosman@comcast-dot-net.invalid "Don't be afraid of work. Make work afraid of you." -- TLM