<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Shriyog Ingale</title>
    <description>The latest articles on DEV Community by Shriyog Ingale (@shriyog).</description>
    <link>https://dev.to/shriyog</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F36040%2F981eb9e6-f273-44dc-8b67-1da20110a90e.png</url>
      <title>DEV Community: Shriyog Ingale</title>
      <link>https://dev.to/shriyog</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/shriyog"/>
    <language>en</language>
    <item>
      <title>Java 8 lambdas: 101</title>
      <dc:creator>Shriyog Ingale</dc:creator>
      <pubDate>Thu, 27 Dec 2018 08:41:28 +0000</pubDate>
      <link>https://dev.to/shriyog/java-8-lambdas-101-1ah3</link>
      <guid>https://dev.to/shriyog/java-8-lambdas-101-1ah3</guid>
      <description>&lt;p&gt;All you need is 5 minutes to grok the Lambda expressions introduced in Java 8. This post acts as a starter by quickly demonstrating how lambdas are super cool in replacing hefty anonymous classes.&lt;/p&gt;

&lt;h2&gt;
  
  
  Anonymous classes
&lt;/h2&gt;

&lt;p&gt;You have used them all over - to implement event handlers in GUIs, to specify a Comparator for sorting, etc.&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    button.setOnAction(new EventHandler&amp;lt;ActionEvent&amp;gt;() {
        @Override
        public void handle(ActionEvent event) {
            System.out.println("Hello World!");
        }
    });
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;By doing this, we are just passing the functionality of handling an event. As in passing code as a method argument.&lt;/p&gt;

&lt;p&gt;The problem with simplest anonymous classes - one with a single abstract method, is the unnecessary boiler-plate syntax going along with it.&lt;/p&gt;

&lt;p&gt;Lambdas exactly solve this by providing a clean &amp;amp; compact syntax to convey the functionality of single method classes.&lt;/p&gt;

&lt;h2&gt;
  
  
  A minimalistic example
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;An interface - &lt;code&gt;Operation&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;A class - &lt;code&gt;Calculator&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The &lt;code&gt;Operation&lt;/code&gt; interface declares a method to operate on 2 integers. This can be implemented by multiple types of operations such as Addition and Subtraction.&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public interface Operation {
    int operate(int a, int b);
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;The static calculate method performs the operation by calling operate method using supplied operands.&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public class Calculator {
    public static int calculate(int a, int b, Operation operation) {
        return operation.operate(a, b);
    }
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;We want to invoke calculate to perform a defined operation(implementation of Operation interface) on given operands.&lt;/p&gt;

&lt;h3&gt;
  
  
  Approach 1 - Using Anonymous class
&lt;/h3&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    Operation additionOperation = new Operation() {
        @Override
        public int operate(int a, int b) {
            return a + b;
        }
    };

    int result = Calculator.calculate(2, 3, additionOperation);
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;h3&gt;
  
  
  Approach 2 - Using lambda expression
&lt;/h3&gt;
&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    int result = Calculator.calculate(2, 3, (x, y) -&amp;gt; x + y);
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;The lambda expression  &lt;strong&gt;&lt;code&gt;(x, y) -&amp;gt; x + y&lt;/code&gt;&lt;/strong&gt; can be dissected into&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;(x, y)&lt;/code&gt;&lt;br&gt;
Parenthesis enclosed comma separated set of formal arguments(same as in the Interface method signature).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;-&amp;gt;&lt;/code&gt;&lt;br&gt;
The arrow to differentiate.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;x + y&lt;/code&gt;&lt;br&gt;
The body which can have an expression which is evaluated and returned or a block of statements enclosed by curly braces.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;This is how lambdas can easily replace those bulky anonymous classes. They also seem to fit perfectly with the new &lt;a href="https://docs.oracle.com/javase/8/docs/api/java/util/stream/package-summary.html"&gt;Java stream API&lt;/a&gt; which is a collection of functional-style transformations.&lt;/p&gt;

&lt;h2&gt;
  
  
  Resources
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://docs.oracle.com/javase/tutorial/java/javaOO/lambdaexpressions.html"&gt;Oracle Java tutorials - Lambda expressions&lt;/a&gt;&lt;/p&gt;

</description>
      <category>java</category>
      <category>java8</category>
      <category>lambda</category>
      <category>beginners</category>
    </item>
  </channel>
</rss>
