{"id":69,"date":"2025-01-20T17:25:50","date_gmt":"2025-01-20T17:25:50","guid":{"rendered":"https:\/\/blogs.sajansthapit.com\/?p=69"},"modified":"2025-01-20T17:54:22","modified_gmt":"2025-01-20T17:54:22","slug":"introduction-to-lambdas","status":"publish","type":"post","link":"https:\/\/blogs.sajansthapit.com\/index.php\/2025\/01\/20\/introduction-to-lambdas\/","title":{"rendered":"Introduction to Lambdas in Java"},"content":{"rendered":"<p><span style=\"font-weight: 400;\">Lambdas were introduced in Java 8, expanding functional programming in the language. They also made code easier to read and maintain. Lambdas played a vital role in reducing boilerplate codes and focusing on behavior rather than structure. But before jumping directly into lambdas, let\u2019s understand a few things.\u00a0<\/span><\/p>\n<h4><\/h4>\n<p>&nbsp;<\/p>\n<h4>Anonymous inner class<\/h4>\n<p>&nbsp;<\/p>\n<p><span style=\"font-weight: 400;\">An anonymous inner class in Java defines and instantiates a class or interface in a single step. Since it is defined within a class and assigned to a variable it doesn\u2019t need to have a name so it\u2019s named \u201canonymous\u201d. Let\u2019s make it clear with an example.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Let\u2019s take a simple example of adding two numbers. I know there are many ways but let us focus on without an anonymous inner class first.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Let\u2019s create an interface with an abstract method that takes two integer parameters and returns the integer.<\/span><\/p>\n<p>&nbsp;<\/p>\n<pre><code class=\"language-java\">public interface Addition {\n     int add(int a, int b);\n}\n<\/code><\/pre>\n<p><span style=\"font-weight: 400;\">Now, let the class implement the interface and add the method body.<\/span><\/p>\n<p>&nbsp;<\/p>\n<pre><code class=\"language-java\">public class AddtionImpl implements Addition{\n    @Override\n    public int add(int a, int b) {\n        return a + b;\n    }\n}<\/code><\/pre>\n<p><span style=\"font-weight: 400;\">Finally, to add two numbers, we create an object of AdditionImpl that extends Addition and calls the add method.<\/span><\/p>\n<p>&nbsp;<\/p>\n<pre><code class=\"language-java\">public class LambdasDemo {\n    public static void main(String[] args) {\n        Addition addition = new AddtionImpl();\n        int sum = addition.add(10, 20);\n        System.out.println(&quot;sum = &quot; + sum);\n    }\n}<\/code><\/pre>\n<pre><\/pre>\n<pre><code class=\"language-\">sum = 30 \/\/output<\/code><\/pre>\n<p>&nbsp;<\/p>\n<hr \/>\n<p>&nbsp;<\/p>\n<p><span style=\"font-weight: 400;\">That\u2019s one way of doing things. The problem here is that it has a lot of boilerplate codes. Let\u2019s try to reduce those codes by using an anonymous inner class.\u00a0<\/span><\/p>\n<p><span style=\"font-weight: 400;\">The interface remains the same but instead of creating AddtionImpl class to add the method body let us define and instantiate it within our main class.<\/span><\/p>\n<p>&nbsp;<\/p>\n<pre><code class=\"language-java\">public class LambdasDemo {\n    public static void main(String[] args) {\n        Addition addition = new Addition() {\n            public int add(int a, int b) {\n                return a + b;\n            }\n        };\n\n        int sum = addition.add(10, 20);\n        System.out.println(&quot;sum = &quot; + sum);\n    }\n}<\/code><\/pre>\n<p>&nbsp;<\/p>\n<pre><code class=\"language-\">sum = 30 \/\/output<\/code><\/pre>\n<p>&nbsp;<\/p>\n<p><span style=\"font-weight: 400;\">Here instead of creating an extra class we defined and instantiated the Addition interface using an anonymous inner class. We don\u2019t need to explicitly name the class.\u00a0<\/span><\/p>\n<p><span style=\"font-weight: 400;\">An anonymous inner class can be used to define and instantiate both interface and class. It is usually created for quick, localized use, such as handling events or creating a single implementation of an interface.\u00a0 However one of the major drawbacks is that it has limited reusability compared to creating a separate class since this anonymous class cannot be referenced elsewhere.<\/span><\/p>\n<p>&nbsp;<\/p>\n<hr \/>\n<p>&nbsp;<\/p>\n<h4><\/h4>\n<h4><\/h4>\n<h4>Lambdas<\/h4>\n<p>&nbsp;<\/p>\n<p>Okay, now things get even more interesting. We have reduced a lot of boilerplate by removing an entire class (AdditionImpl). But what if we could eliminate the extra boilerplate code, such as the anonymous inner class?<br \/>\nTo remove the extra boilerplate code Java 8 introduced the concept of lambdas. First let\u2019s look at an example.<\/p>\n<p>&nbsp;<\/p>\n<pre><code class=\"language-java\">public class LambdasDemo {\n    public static void main(String[] args) {\n        Addition addition = (a, b) -&gt; a + b;\n        int sum = addition.add(10, 20);\n        System.out.println(&quot;sum = &quot; + sum);\n    }\n}<\/code><\/pre>\n<pre><\/pre>\n<pre><code class=\"language-\">sum = 30 \/\/ouput<\/code><\/pre>\n<p><span style=\"font-weight: 400;\">The code has been reduced to a single line. Instead of implementing an interface, we\u2019re passing a block of code &#8211; a function without even a name just (a, b) as parameters. In lambda expression -&gt; separates the parameter from the body, which is just some code that is run when someone calls the add method.<\/span><\/p>\n<p>&nbsp;<\/p>\n<hr \/>\n<p style=\"text-align: center;\">Happy Coding!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Lambdas were introduced in Java 8, expanding functional programming in the language. They also made code easier to read and maintain. Lambdas played a vital role in reducing boilerplate codes and focusing on behavior rather than structure. But before jumping directly into lambdas, let\u2019s understand a few things.\u00a0 &nbsp; Anonymous inner class &nbsp; An anonymous [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[3,9,8,7],"class_list":["post-69","post","type-post","status-publish","format-standard","hentry","category-java","tag-java","tag-java-8","tag-java-core","tag-lambda"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v24.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Introduction to Lambdas in Java - blogs.sajansthapit.com<\/title>\n<meta name=\"description\" content=\"Let&#039;s understand on of the key features introduced in Java 8 - The lambda expression in detail for complete beginners...\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/blogs.sajansthapit.com\/index.php\/2025\/01\/20\/introduction-to-lambdas\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Introduction to Lambdas in Java - blogs.sajansthapit.com\" \/>\n<meta property=\"og:description\" content=\"Let&#039;s understand on of the key features introduced in Java 8 - The lambda expression in detail for complete beginners...\" \/>\n<meta property=\"og:url\" content=\"https:\/\/blogs.sajansthapit.com\/index.php\/2025\/01\/20\/introduction-to-lambdas\/\" \/>\n<meta property=\"og:site_name\" content=\"blogs.sajansthapit.com\" \/>\n<meta property=\"article:published_time\" content=\"2025-01-20T17:25:50+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2025-01-20T17:54:22+00:00\" \/>\n<meta name=\"author\" content=\"admin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/blogs.sajansthapit.com\/index.php\/2025\/01\/20\/introduction-to-lambdas\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/blogs.sajansthapit.com\/index.php\/2025\/01\/20\/introduction-to-lambdas\/\"},\"author\":{\"name\":\"admin\",\"@id\":\"https:\/\/blogs.sajansthapit.com\/#\/schema\/person\/5681d9d4121ccc7789717f808e8edcc4\"},\"headline\":\"Introduction to Lambdas in Java\",\"datePublished\":\"2025-01-20T17:25:50+00:00\",\"dateModified\":\"2025-01-20T17:54:22+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/blogs.sajansthapit.com\/index.php\/2025\/01\/20\/introduction-to-lambdas\/\"},\"wordCount\":460,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/blogs.sajansthapit.com\/#\/schema\/person\/5681d9d4121ccc7789717f808e8edcc4\"},\"keywords\":[\"java\",\"Java 8\",\"Java-core\",\"Lambda\"],\"articleSection\":[\"Java\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/blogs.sajansthapit.com\/index.php\/2025\/01\/20\/introduction-to-lambdas\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/blogs.sajansthapit.com\/index.php\/2025\/01\/20\/introduction-to-lambdas\/\",\"url\":\"https:\/\/blogs.sajansthapit.com\/index.php\/2025\/01\/20\/introduction-to-lambdas\/\",\"name\":\"Introduction to Lambdas in Java - blogs.sajansthapit.com\",\"isPartOf\":{\"@id\":\"https:\/\/blogs.sajansthapit.com\/#website\"},\"datePublished\":\"2025-01-20T17:25:50+00:00\",\"dateModified\":\"2025-01-20T17:54:22+00:00\",\"description\":\"Let's understand on of the key features introduced in Java 8 - The lambda expression in detail for complete beginners...\",\"breadcrumb\":{\"@id\":\"https:\/\/blogs.sajansthapit.com\/index.php\/2025\/01\/20\/introduction-to-lambdas\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/blogs.sajansthapit.com\/index.php\/2025\/01\/20\/introduction-to-lambdas\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/blogs.sajansthapit.com\/index.php\/2025\/01\/20\/introduction-to-lambdas\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/blogs.sajansthapit.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Introduction to Lambdas in Java\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/blogs.sajansthapit.com\/#website\",\"url\":\"https:\/\/blogs.sajansthapit.com\/\",\"name\":\"blogs.sajansthapit.com\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\/\/blogs.sajansthapit.com\/#\/schema\/person\/5681d9d4121ccc7789717f808e8edcc4\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/blogs.sajansthapit.com\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\/\/blogs.sajansthapit.com\/#\/schema\/person\/5681d9d4121ccc7789717f808e8edcc4\",\"name\":\"admin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/blogs.sajansthapit.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/d17a183aedd86cc508635e48ed228c44aba311ccb906c855852386ead5cb687c?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/d17a183aedd86cc508635e48ed228c44aba311ccb906c855852386ead5cb687c?s=96&d=mm&r=g\",\"caption\":\"admin\"},\"logo\":{\"@id\":\"https:\/\/blogs.sajansthapit.com\/#\/schema\/person\/image\/\"},\"sameAs\":[\"https:\/\/blogs.sajansthapit.com\"],\"url\":\"https:\/\/blogs.sajansthapit.com\/index.php\/author\/sajansthapit\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Introduction to Lambdas in Java - blogs.sajansthapit.com","description":"Let's understand on of the key features introduced in Java 8 - The lambda expression in detail for complete beginners...","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/blogs.sajansthapit.com\/index.php\/2025\/01\/20\/introduction-to-lambdas\/","og_locale":"en_US","og_type":"article","og_title":"Introduction to Lambdas in Java - blogs.sajansthapit.com","og_description":"Let's understand on of the key features introduced in Java 8 - The lambda expression in detail for complete beginners...","og_url":"https:\/\/blogs.sajansthapit.com\/index.php\/2025\/01\/20\/introduction-to-lambdas\/","og_site_name":"blogs.sajansthapit.com","article_published_time":"2025-01-20T17:25:50+00:00","article_modified_time":"2025-01-20T17:54:22+00:00","author":"admin","twitter_card":"summary_large_image","twitter_misc":{"Written by":"admin","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/blogs.sajansthapit.com\/index.php\/2025\/01\/20\/introduction-to-lambdas\/#article","isPartOf":{"@id":"https:\/\/blogs.sajansthapit.com\/index.php\/2025\/01\/20\/introduction-to-lambdas\/"},"author":{"name":"admin","@id":"https:\/\/blogs.sajansthapit.com\/#\/schema\/person\/5681d9d4121ccc7789717f808e8edcc4"},"headline":"Introduction to Lambdas in Java","datePublished":"2025-01-20T17:25:50+00:00","dateModified":"2025-01-20T17:54:22+00:00","mainEntityOfPage":{"@id":"https:\/\/blogs.sajansthapit.com\/index.php\/2025\/01\/20\/introduction-to-lambdas\/"},"wordCount":460,"commentCount":0,"publisher":{"@id":"https:\/\/blogs.sajansthapit.com\/#\/schema\/person\/5681d9d4121ccc7789717f808e8edcc4"},"keywords":["java","Java 8","Java-core","Lambda"],"articleSection":["Java"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/blogs.sajansthapit.com\/index.php\/2025\/01\/20\/introduction-to-lambdas\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/blogs.sajansthapit.com\/index.php\/2025\/01\/20\/introduction-to-lambdas\/","url":"https:\/\/blogs.sajansthapit.com\/index.php\/2025\/01\/20\/introduction-to-lambdas\/","name":"Introduction to Lambdas in Java - blogs.sajansthapit.com","isPartOf":{"@id":"https:\/\/blogs.sajansthapit.com\/#website"},"datePublished":"2025-01-20T17:25:50+00:00","dateModified":"2025-01-20T17:54:22+00:00","description":"Let's understand on of the key features introduced in Java 8 - The lambda expression in detail for complete beginners...","breadcrumb":{"@id":"https:\/\/blogs.sajansthapit.com\/index.php\/2025\/01\/20\/introduction-to-lambdas\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/blogs.sajansthapit.com\/index.php\/2025\/01\/20\/introduction-to-lambdas\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/blogs.sajansthapit.com\/index.php\/2025\/01\/20\/introduction-to-lambdas\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/blogs.sajansthapit.com\/"},{"@type":"ListItem","position":2,"name":"Introduction to Lambdas in Java"}]},{"@type":"WebSite","@id":"https:\/\/blogs.sajansthapit.com\/#website","url":"https:\/\/blogs.sajansthapit.com\/","name":"blogs.sajansthapit.com","description":"","publisher":{"@id":"https:\/\/blogs.sajansthapit.com\/#\/schema\/person\/5681d9d4121ccc7789717f808e8edcc4"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/blogs.sajansthapit.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":["Person","Organization"],"@id":"https:\/\/blogs.sajansthapit.com\/#\/schema\/person\/5681d9d4121ccc7789717f808e8edcc4","name":"admin","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/blogs.sajansthapit.com\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/d17a183aedd86cc508635e48ed228c44aba311ccb906c855852386ead5cb687c?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/d17a183aedd86cc508635e48ed228c44aba311ccb906c855852386ead5cb687c?s=96&d=mm&r=g","caption":"admin"},"logo":{"@id":"https:\/\/blogs.sajansthapit.com\/#\/schema\/person\/image\/"},"sameAs":["https:\/\/blogs.sajansthapit.com"],"url":"https:\/\/blogs.sajansthapit.com\/index.php\/author\/sajansthapit\/"}]}},"_links":{"self":[{"href":"https:\/\/blogs.sajansthapit.com\/index.php\/wp-json\/wp\/v2\/posts\/69","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blogs.sajansthapit.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.sajansthapit.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.sajansthapit.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.sajansthapit.com\/index.php\/wp-json\/wp\/v2\/comments?post=69"}],"version-history":[{"count":18,"href":"https:\/\/blogs.sajansthapit.com\/index.php\/wp-json\/wp\/v2\/posts\/69\/revisions"}],"predecessor-version":[{"id":87,"href":"https:\/\/blogs.sajansthapit.com\/index.php\/wp-json\/wp\/v2\/posts\/69\/revisions\/87"}],"wp:attachment":[{"href":"https:\/\/blogs.sajansthapit.com\/index.php\/wp-json\/wp\/v2\/media?parent=69"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.sajansthapit.com\/index.php\/wp-json\/wp\/v2\/categories?post=69"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.sajansthapit.com\/index.php\/wp-json\/wp\/v2\/tags?post=69"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}