<?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: Sangharsha Chaulagain</title>
    <description>The latest articles on DEV Community by Sangharsha Chaulagain (@csangharsha).</description>
    <link>https://dev.to/csangharsha</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%2F231086%2F01d02e46-f5ad-420d-b3fd-18020cc8d6d9.jpg</url>
      <title>DEV Community: Sangharsha Chaulagain</title>
      <link>https://dev.to/csangharsha</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/csangharsha"/>
    <language>en</language>
    <item>
      <title>Java 8 - Functional Interface</title>
      <dc:creator>Sangharsha Chaulagain</dc:creator>
      <pubDate>Fri, 10 Apr 2020 14:19:26 +0000</pubDate>
      <link>https://dev.to/csangharsha/java-8-functional-interface-2cm9</link>
      <guid>https://dev.to/csangharsha/java-8-functional-interface-2cm9</guid>
      <description>&lt;p&gt;In the previous post of this series, we discussed what is lambda expression and how can we write lambda expression. In this post, we will discuss how we can execute lambda expression. As I said, at the end of the last post, we can execute lambda expression by using Java 8's new feature, &lt;strong&gt;Functional Interface&lt;/strong&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  So what is Functional Interface?
&lt;/h2&gt;

&lt;p&gt;The interface containing the single abstract method(SAM) are called Functional Interface. Some of the Functional Interface we may know are: &lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Interface&lt;/th&gt;
&lt;th&gt;method&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Runnable&lt;/td&gt;
&lt;td&gt;run()&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Callable&lt;/td&gt;
&lt;td&gt;call()&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Comparable&lt;/td&gt;
&lt;td&gt;compareTo()&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;ActionListener&lt;/td&gt;
&lt;td&gt;actionPerformed()&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;In version 1.8, java introduced one annotation &lt;code&gt;@FunctionalInterface&lt;/code&gt; to specify interface as functional interface explicitly. But the annotation is optional. However, it is recommended to write the annotation. It helps prevent any additional abstract method. If the interface with annotation consists of more than one abstract method, then we get a compile-time error saying:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;error: Unexpected @FunctionalInterface annotation
@FunctionalInterface
^
  CustomInterface is not a functional interface
    multiple non-overriding abstract methods found in interface CustomInterface
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; Functional interface can have multiple default and static method.&lt;/p&gt;

&lt;p&gt;Example 1:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@FunctionalInterface
interface A {
    void m1();
} 

@FunctionalInterface
interface B extends A {
}

@FunctionalInterface
interface C extends A {
    void m1();
}

@FunctionalInterface
interface D extends A {
    void m2();
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the above example, the interface D definition is invalid since it contains two methods: m1() [extended from interface A] and m2().&lt;/p&gt;

&lt;p&gt;In the next part, I will write about how lambda expression can be used with the functional interface.&lt;/p&gt;

</description>
      <category>java</category>
      <category>java8</category>
      <category>functional</category>
    </item>
    <item>
      <title>Java 8 - Lambda Expression Rule</title>
      <dc:creator>Sangharsha Chaulagain</dc:creator>
      <pubDate>Thu, 19 Mar 2020 17:57:49 +0000</pubDate>
      <link>https://dev.to/csangharsha/java-8-lambda-expression-rule-5am6</link>
      <guid>https://dev.to/csangharsha/java-8-lambda-expression-rule-5am6</guid>
      <description>&lt;p&gt;There are some rules to make lambda expression more concise.&lt;/p&gt;

&lt;h4&gt;
  
  
  Example 1:
&lt;/h4&gt;

&lt;p&gt;Let's take an example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;displaySum&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The valid Lambda expression of above method is:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It can be made more concise by taking out the parameter's datatypes.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;&lt;em&gt;Note: The type of the parameters can be declared explicitly, or it can be inferred from the context.&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;




&lt;h4&gt;
  
  
  Example 2:
&lt;/h4&gt;

&lt;p&gt;Let's take another example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;square&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Its equivalent lambda expression is:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;&lt;em&gt;Note:&lt;/em&gt;&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;&lt;em&gt;1. If there are no curly brackets, then the return keyword is not needed.&lt;/em&gt;&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;&lt;em&gt;2. If there is only one parameter in the function, then () is also not needed.&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;


&lt;h5&gt;
  
  
  Q1. Write an lambda expression that returns length of string.
&lt;/h5&gt;



&lt;p&gt;&lt;code&gt;Give answer on the comment below :)&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;




&lt;h4&gt;
  
  
  How to execute the Lambda expression?
&lt;/h4&gt;

&lt;p&gt;To execute the lambda expression, we use one of the other features of java 8: &lt;strong&gt;Functional Interface&lt;/strong&gt;. We will discuss it in the next post of this series.&lt;/p&gt;




</description>
      <category>java</category>
      <category>lambda</category>
      <category>functional</category>
    </item>
    <item>
      <title>Java 8 - Lambda Expression 101</title>
      <dc:creator>Sangharsha Chaulagain</dc:creator>
      <pubDate>Thu, 19 Mar 2020 17:57:11 +0000</pubDate>
      <link>https://dev.to/csangharsha/java-8-lambda-expression-101-17o8</link>
      <guid>https://dev.to/csangharsha/java-8-lambda-expression-101-17o8</guid>
      <description>&lt;p&gt;One of the biggest changes in Java 8 was lambda expression. The main reason to bring lambda expression to Java is to bring the benefit of functional programming into Java. &lt;/p&gt;

&lt;h2&gt;
  
  
  What is Lambda Expression?
&lt;/h2&gt;

&lt;p&gt;It is a &lt;strong&gt;concise&lt;/strong&gt; representation of &lt;strong&gt;anonymous functions&lt;/strong&gt; that can be &lt;strong&gt;passed around&lt;/strong&gt;. Unlike methods, it is nameless and is not associated with the particular class. It can be passed as an argument to a method or stored in a variable. Lambda expression is said to be a concise representation since there is no need to write boilerplate code as we do for anonymous classes.&lt;/p&gt;

&lt;h2&gt;
  
  
  Convert method into Lambda Expression
&lt;/h2&gt;

&lt;p&gt;It is really very easy to convert a method into a lambda expression. Following are some rules:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Remove the name&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Remove the return type&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Remove the modifier&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Add the arrow symbol(-&amp;gt;)&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  Example 1:
&lt;/h4&gt;

&lt;p&gt;Let’s take the following method,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;sayHello&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="err"&gt;“&lt;/span&gt;&lt;span class="nc"&gt;Hello&lt;/span&gt;&lt;span class="err"&gt;”&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After removing the name, the return type and the modifier from the above method,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="err"&gt;“&lt;/span&gt;&lt;span class="nc"&gt;Hello&lt;/span&gt;&lt;span class="err"&gt;”&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But to make it a valid lambda expression, we need to add a special symbol, &lt;br&gt;
 -&amp;gt;. Hence the above method becomes:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt; &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="err"&gt;“&lt;/span&gt;&lt;span class="nc"&gt;Hello&lt;/span&gt;&lt;span class="err"&gt;”&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt; &lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The above is the valid lambda expression. But we can concise the above lambda expression even more. If we have only one statement in the body of lambda expression, curly brackets are optional.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="err"&gt;“&lt;/span&gt;&lt;span class="nc"&gt;Hello&lt;/span&gt;&lt;span class="err"&gt;”&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;&lt;em&gt;Note: If multiple statements are in the body of the lambda expression then the curly bracket is mandatory.&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;




</description>
      <category>java</category>
      <category>lambdaexpression</category>
      <category>functional</category>
    </item>
  </channel>
</rss>
