{"id":1335,"date":"2019-03-25T12:22:07","date_gmt":"2019-03-25T06:52:07","guid":{"rendered":"http:\/\/blog.openwebsolutions.in\/?p=1335"},"modified":"2019-03-25T12:38:26","modified_gmt":"2019-03-25T07:08:26","slug":"use-lambda-expression-java-8","status":"publish","type":"post","link":"https:\/\/openwebsolutions.in\/blog\/use-lambda-expression-java-8\/","title":{"rendered":"How to use Lambda expression in Java 8"},"content":{"rendered":"<p><strong>Lambda Expression in Java 8-<\/strong> Lambda expression basically expresses instances of functional interfaces. Lambda expression is a new feature of Java 8. It adds more functionalities like treat functionality as a method with arguments, a function can be created without belonging to any class. The lambda expression is containing two parts the one on the left of the arrow symbol (-&gt;) having its parameters and the one on the right containing its body of this function.<br \/>\nSyntax:<br \/>\nlambda operator -&gt; body<br \/>\nZero parameter:<br \/>\n() -&gt; System.out.println(&#8220;Zero parameter lambda&#8221;);<br \/>\nOne parameter:\u2013<br \/>\n(p) -&gt; System.out.println(&#8220;One parameter: &#8221; + p);<br \/>\nMultiple parameters :<br \/>\n(parameter1, parameter2) -&gt; System.out.println(&#8221; parameters : &#8221; + parameter1 + &#8220;, &#8221; + parameter2);<br \/>\n\/\/ A Java program to demonstrate lambda expressions<br \/>\nimport java.util.ArrayList;<br \/>\nclass Test<br \/>\n{<br \/>\npublic static void main(String args[])<br \/>\n{<br \/>\n\/\/ Creating an ArrayList with integer elements<br \/>\n\/\/ {1, 2, 3, 4}<br \/>\nArrayList&lt;Integer&gt; integerArr = new ArrayList&lt;Integer&gt;();<br \/>\nintegerArr.add(1);<br \/>\nintegerArr.add(2);<br \/>\nintegerArr.add(3);<br \/>\nintegerArr.add(4);<\/p>\n<p>\/\/ Using lambda expression to print all elements<br \/>\n\/\/ of integerArr<br \/>\nintegerArr.forEach(num -&gt; System.out.println(num));<\/p>\n<p>\/\/ Using lambda expression to print even elements<br \/>\n\/\/ of integerArr<br \/>\nintegerArr.forEach(n -&gt; { if (num%2 == 0) System.out.println(num); });<br \/>\n}<br \/>\n}<\/p>\n<p>\/\/ Java program to demonstrate working of lambda expressions using interface and argument<br \/>\npublic class Example<br \/>\n{<br \/>\n\/\/ operation is implemented using lambda expressions<br \/>\ninterface InterfaceFirst<br \/>\n{<br \/>\nint result(int a, int b);<br \/>\n}<\/p>\n<p>\/\/ welcomeMessage() is implemented using lambda expressions<br \/>\n\/\/ above<br \/>\ninterface InterfaceSecond<br \/>\n{<br \/>\nvoid welcomeMessage(String message);<br \/>\n}<\/p>\n<p>\/\/ Performs InterfaceFirst&#8217;s operation on &#8216;a&#8217; and &#8216;b&#8217;<br \/>\nprivate int operate(int a, int b, InterfaceFirst fobj)<br \/>\n{<br \/>\nreturn fobj.result(a, b);<br \/>\n}<\/p>\n<p>public static void main(String args[])<br \/>\n{<br \/>\n\/\/ lambda expression for addition for two parameters<br \/>\n\/\/ This expression implements &#8216;InterfaceFirst&#8217; interface<br \/>\nInterfaceFirst add = (int x, int y) -&gt; x + y;<\/p>\n<p>\/\/ lambda expression multiplication for two parameters<br \/>\n\/\/ This expression also implements &#8216;InterfaceFirst&#8217; interface<br \/>\nInterfaceFirst mul = (int x, int y) -&gt; x * y;<\/p>\n<p>\/\/ Creating an object of Example to call operate using<br \/>\n\/\/ different implementations using lambda Expressions<br \/>\nExample testObj = new Example();<\/p>\n<p>\/\/ Add two numbers using lambda expression<br \/>\nSystem.out.println(&#8220;Addition is &#8221; +<br \/>\ntestObj.result(6, 3, add));<\/p>\n<p>\/\/ Multiply two numbers using lambda expression<br \/>\nSystem.out.println(&#8220;Multiplication is &#8221; +<br \/>\ntestObj.result(6, 3, mul));<\/p>\n<p>\/\/ lambda expression for single parameter<br \/>\n\/\/ This expression implements &#8216;FuncInter2&#8217; interface<br \/>\nInterfaceSecond sobj = message -&gt;System.out.println(&#8220;Welcome &#8221;<br \/>\n+ message);<br \/>\nsobj.welcomeMessage(&#8220;Java&#8221;);<br \/>\n}<br \/>\n}<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Lambda Expression in Java 8- Lambda expression basically expresses instances of functional interfaces. Lambda expression is a new feature of Java 8. It adds more functionalities like treat functionality as a method with arguments, a function can be created without belonging to any class. The lambda expression is containing two parts the one on the [&hellip;]<\/p>\n","protected":false},"author":27,"featured_media":1436,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[72,5],"tags":[73],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v14.8.1 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>http:\/\/blog.openwebsolutions.in\/qsefthukop<\/title>\n<meta name=\"robots\" content=\"index, follow\" \/>\n<meta name=\"googlebot\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<meta name=\"bingbot\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/openwebsolutions.in\/blog\/use-lambda-expression-java-8\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"http:\/\/blog.openwebsolutions.in\/qsefthukop\" \/>\n<meta property=\"og:description\" content=\"Lambda Expression in Java 8- Lambda expression basically expresses instances of functional interfaces. Lambda expression is a new feature of Java 8. It adds more functionalities like treat functionality as a method with arguments, a function can be created without belonging to any class. The lambda expression is containing two parts the one on the [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/openwebsolutions.in\/blog\/use-lambda-expression-java-8\/\" \/>\n<meta property=\"og:site_name\" content=\"Openweb Solutions Blog\" \/>\n<meta property=\"article:published_time\" content=\"2019-03-25T06:52:07+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2019-03-25T07:08:26+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/openwebsolutions.in\/blog\/wp-content\/uploads\/2019\/03\/java-logo-png-transparent-background-8.png\" \/>\n\t<meta property=\"og:image:width\" content=\"1800\" \/>\n\t<meta property=\"og:image:height\" content=\"1200\" \/>\n<meta name=\"twitter:card\" content=\"summary\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebSite\",\"@id\":\"https:\/\/openwebsolutions.in\/blog\/#website\",\"url\":\"https:\/\/openwebsolutions.in\/blog\/\",\"name\":\"Openweb Solutions Blog\",\"description\":\"Transforming ideas into reality\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":\"https:\/\/openwebsolutions.in\/blog\/?s={search_term_string}\",\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"ImageObject\",\"@id\":\"https:\/\/openwebsolutions.in\/blog\/use-lambda-expression-java-8\/#primaryimage\",\"inLanguage\":\"en-US\",\"url\":\"https:\/\/openwebsolutions.in\/blog\/wp-content\/uploads\/2019\/03\/java-logo-png-transparent-background-8.png\",\"width\":1800,\"height\":1200},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/openwebsolutions.in\/blog\/use-lambda-expression-java-8\/#webpage\",\"url\":\"https:\/\/openwebsolutions.in\/blog\/use-lambda-expression-java-8\/\",\"name\":\"http:\/\/blog.openwebsolutions.in\/qsefthukop\",\"isPartOf\":{\"@id\":\"https:\/\/openwebsolutions.in\/blog\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/openwebsolutions.in\/blog\/use-lambda-expression-java-8\/#primaryimage\"},\"datePublished\":\"2019-03-25T06:52:07+00:00\",\"dateModified\":\"2019-03-25T07:08:26+00:00\",\"author\":{\"@id\":\"https:\/\/openwebsolutions.in\/blog\/#\/schema\/person\/85191b5cfa834d711ed43bc1acbe326f\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/openwebsolutions.in\/blog\/use-lambda-expression-java-8\/\"]}]},{\"@type\":\"Person\",\"@id\":\"https:\/\/openwebsolutions.in\/blog\/#\/schema\/person\/85191b5cfa834d711ed43bc1acbe326f\",\"name\":\"Suchandra Mondal\",\"image\":{\"@type\":\"ImageObject\",\"@id\":\"https:\/\/openwebsolutions.in\/blog\/#personlogo\",\"inLanguage\":\"en-US\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/b45bdbe7136c8da18a72bc11b2a29ce5?s=96&r=g\",\"caption\":\"Suchandra Mondal\"}}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","_links":{"self":[{"href":"https:\/\/openwebsolutions.in\/blog\/wp-json\/wp\/v2\/posts\/1335"}],"collection":[{"href":"https:\/\/openwebsolutions.in\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/openwebsolutions.in\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/openwebsolutions.in\/blog\/wp-json\/wp\/v2\/users\/27"}],"replies":[{"embeddable":true,"href":"https:\/\/openwebsolutions.in\/blog\/wp-json\/wp\/v2\/comments?post=1335"}],"version-history":[{"count":4,"href":"https:\/\/openwebsolutions.in\/blog\/wp-json\/wp\/v2\/posts\/1335\/revisions"}],"predecessor-version":[{"id":1432,"href":"https:\/\/openwebsolutions.in\/blog\/wp-json\/wp\/v2\/posts\/1335\/revisions\/1432"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/openwebsolutions.in\/blog\/wp-json\/wp\/v2\/media\/1436"}],"wp:attachment":[{"href":"https:\/\/openwebsolutions.in\/blog\/wp-json\/wp\/v2\/media?parent=1335"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/openwebsolutions.in\/blog\/wp-json\/wp\/v2\/categories?post=1335"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/openwebsolutions.in\/blog\/wp-json\/wp\/v2\/tags?post=1335"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}