8000 java8-experiment/README.md at main · chinmay-anand/java8-experiment · GitHub
[go: up one dir, main page]

Skip to content
{"payload":{"allShortcutsEnabled":false,"fileTree":{"":{"items":[{"name":"src","path":"src","contentType":"directory"},{"name":".gitignore","path":".gitignore","contentType":"file"},{"name":"README.md","path":"README.md","contentType":"file"},{"name":"README_Lambda.md","path":"README_Lambda.md","contentType":"file"}],"totalCount":4}},"fileTreeProcessingTime":8.49417,"foldersToFetch":[],"incompleteFileTree":false,"repo":{"id":349686977,"defaultBranch":"main","name":"java8-experiment","ownerLogin":"chinmay-anand","currentUserCanPush":false,"isFork":false,"isEmpty":false,"createdAt":"2021-03-20T09:57:26.000Z","ownerAvatar":"https://avatars.githubusercontent.com/u/34304780?v=4","public":true,"private":false,"isOrgOwned":false},"codeLineWrapEnabled":false,"symbolsExpanded":false,"treeExpanded":true,"refInfo":{"name":"main","listCacheKey":"v0:1616234385.881033","canEdit":false,"refType":"branch","currentOid":"b7ae49af16961a19b9e77347f4153f0beddb06a1"},"path":"README.md","currentUser":null,"blob":{"rawLines":null,"stylingDirectives":null,"colorizedLines":null,"csv":null,"csvError":null,"dependabotInfo":{"showConfigurationBanner":false,"configFilePath":null,"networkDependabotPath":"/chinmay-anand/java8-experiment/network/updates","dismissConfigurationNoticePath":"/settings/dismiss-notice/dependabot_configuration_notice","configurationNoticeDismissed":null},"displayName":"README.md","displayUrl":"https://github.com/chinmay-anand/java8-experiment/blob/main/README.md?raw=true","headerInfo":{"blobSize":"6.8 KB","deleteTooltip":"You must be signed in to make or propose changes","editTooltip":"You must be signed in to make or propose changes","ghDesktopPath":"https://desktop.github.com","isGitLfs":false,"onBranch":true,"shortPath":"a7cb36e","siteNavLoginPath":"/login?return_to=https%3A%2F%2Fgithub.com%2Fchinmay-anand%2Fjava8-experiment%2Fblob%2Fmain%2FREADME.md","isCSV":false,"isRichtext":true,"toc":[{"level":1,"text":"README.md","anchor":"readmemd","htmlText":"README.md"},{"level":1,"text":"Contents of \"java8lambda\" project","anchor":"contents-of-java8lambda-project","htmlText":"Contents of \"java8lambda\" project"},{"level":5,"text":"Advantages of Lambdas:","anchor":"advantages-of-lambdas","htmlText":"Advantages of Lambdas:"},{"level":5,"text":"Functional vs OOP","anchor":"functional-vs-oop","htmlText":"Functional vs OOP"},{"level":5,"text":"Functional Interface","anchor":"functional-interface","htmlText":"Functional Interface"},{"level":5,"text":"How to execute a Lambda Expression","anchor":"how-to-execute-a-lambda-expression","htmlText":"How to execute a Lambda Expression"},{"level":5,"text":"How to convert a normal function into an Lambda function Expression and how to execute it (with example)","anchor":"how-to-convert-a-normal-function-into-an-lambda-function-expression-and-how-to-execute-it-with-example","htmlText":"How to convert a normal function into an Lambda function Expression and how to execute it (with example)"}],"lineInfo":{"truncatedLoc":"102","truncatedSloc":"94"},"mode":"file"},"image":false,"isCodeownersFile":null,"isPlain":false,"isValidLegacyIssueTemplate":false,"issueTemplate":null,"discussionTemplate":null,"language":"Markdown","languageID":222,"large":false,"planSupportInfo":{"repoIsFork":null,"repoOwnedByCurrentUser":null,"requestFullPath":"/chinmay-anand/java8-experiment/blob/main/README.md","showFreeOrgGatedFeatureMessage":null,"showPlanSupportBanner":null,"upgradeDataAttributes":null,"upgradePath":null},"publishBannersInfo":{"dismissActionNoticePath":"/settings/dismiss-notice/publish_action_from_dockerfile","releasePath":"/chinmay-anand/java8-experiment/releases/new?marketplace=true","showPublishActionBanner":false},"rawBlobUrl":"https://github.com/chinmay-anand/java8-experiment/raw/refs/heads/main/README.md","renderImageOrRaw":false,"richText":"\u003carticle class=\"markdown-body entry-content container-lg\" itemprop=\"text\"\u003e\u003cdiv class=\"markdown-heading\" dir=\"auto\"\u003e\u003ch1 tabindex=\"-1\" class=\"heading-element\" dir=\"auto\"\u003eREADME.md\u003c/h1\u003e\u003ca id=\"user-content-readmemd\" class=\"anchor\" aria-label=\"Permalink: README.md\" href=\"#readmemd\"\u003e\u003csvg class=\"octicon octicon-link\" viewBox=\"0 0 16 16\" version=\"1.1\" width=\"16\" height=\"16\" aria-hidden=\"true\"\u003e\u003cpath d=\"m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z\"\u003e\u003c/path\u003e\u003c/svg\u003e\u003c/a\u003e\u003c/div\u003e\n\u003cp dir=\"auto\"\u003eThis repository contains my experiments using java8 - lambda, functional programming and more.. Keep a watch on it.\u003c/p\u003e\n\u003cdiv class=\"markdown-heading\" dir=\"auto\"\u003e\u003ch1 tabindex=\"-1\" class=\"heading-element\" dir=\"auto\"\u003eContents of \"java8lambda\" project\u003c/h1\u003e\u003ca id=\"user-content-contents-of-java8lambda-project\" class=\"anchor\" aria-label=\"Permalink: Contents of \u0026quot;java8lambda\u0026quot; project\" href=\"#contents-of-java8lambda-project\"\u003e\u003csvg class=\"octicon octicon-link\" viewBox=\"0 0 16 16\" version=\"1.1\" width=\"16\" height=\"16\" aria-hidden=\"true\"\u003e\u003cpath d=\"m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z\"\u003e\u003c/path\u003e\u003c/svg\u003e\u003c/a\u003e\u003c/div\u003e\n\u003cul dir=\"auto\"\u003e\n\u003cli\u003eStream API: Refer \"src/com/chinmay/java8lambda/\"\u003c/li\u003e\n\u003cli\u003eUsing Lambdas\u003c/li\u003e\n\u003cli\u003eFunctional Interfaces\u003c/li\u003e\n\u003cli\u003eMethod references\u003c/li\u003e\n\u003cli\u003eCollections improvements\u003c/li\u003e\n\u003c/ul\u003e\n\u003cdiv class=\"markdown-heading\" dir=\"auto\"\u003e\u003ch5 tabindex=\"-1\" class=\"heading-element\" dir=\"auto\"\u003eAdvantages of Lambdas:\u003c/h5\u003e\u003ca id=\"user-content-advantages-of-lambdas\" class=\"anchor\" aria-label=\"Permalink: Advantages of Lambdas:\" href=\"#advantages-of-lambdas\"\u003e\u003csvg class=\"octicon octicon-link\" viewBox=\"0 0 16 16\" version=\"1.1\" width=\"16\" height=\"16\" aria-hidden=\"true\"\u003e\u003cpath d=\"m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z\"\u003e\u003c/path\u003e\u003c/svg\u003e\u003c/a\u003e\u003c/div\u003e\n\u003cul dir=\"auto\"\u003e\n\u003cli\u003eEnables functional programming\n\u003cul dir=\"auto\"\u003e\n\u003cli\u003ewe can pass a function name to a method to get it called on a certain condition.\u003c/li\u003e\n\u003c/ul\u003e\n\u003c/li\u003e\n\u003cli\u003eReadable and concise code\u003c/li\u003e\n\u003cli\u003eEasier-to-use APIs and libraries\u003c/li\u003e\n\u003cli\u003eEnables support for parallel processing\u003c/li\u003e\n\u003cli\u003eLambda works Java8 onwards.\u003c/li\u003e\n\u003c/ul\u003e\n\u003cdiv class=\"markdown-heading\" dir=\"auto\"\u003e\u003ch5 tabindex=\"-1\" class=\"heading-element\" dir=\"auto\"\u003eFunctional vs OOP\u003c/h5\u003e\u003ca id=\"user-content-functional-vs-oop\" class=\"anchor\" aria-label=\"Permalink: Functional vs OOP\" href=\"#functional-vs-oop\"\u003e\u003csvg class=\"octicon octicon-link\" viewBox=\"0 0 16 16\" version=\"1.1\" width=\"16\" height=\"16\" aria-hidden=\"true\"\u003e\u003cpath d=\"m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z\"\u003e\u003c/path\u003e\u003c/svg\u003e\u003c/a\u003e\u003c/div\u003e\n\u003cul dir=\"auto\"\u003e\n\u003cli\u003eBoth can achieve the same\u003c/li\u003e\n\u003cli\u003eFunctional programmign gives a more readable and hence more maintainable code.\u003c/li\u003e\n\u003cli\u003eFunctional programming is elegant in certain situatio, but not always. We may need to use Object oriented programming in some other cases.\u003c/li\u003e\n\u003cli\u003eIn OOP all code blocks are \"associated\" with classes and objects.\u003c/li\u003e\n\u003c/ul\u003e\n\u003cdiv class=\"markdown-heading\" dir=\"auto\"\u003e\u003ch5 tabindex=\"-1\" class=\"heading-element\" dir=\"auto\"\u003eFunctional Interface\u003c/h5\u003e\u003ca id=\"user-content-functional-interface\" class=\"anchor\" aria-label=\"Permalink: Functional Interface\" href=\"#functional-interface\"\u003e\u003csvg class=\"octicon octicon-link\" viewBox=\"0 0 16 16\" version=\"1.1\" width=\"16\" height=\"16\" aria-hidden=\"true\"\u003e\u003cpath d=\"m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z\"\u003e\u003c/path\u003e\u003c/svg\u003e\u003c/a\u003e\u003c/div\u003e\n\u003cdiv class=\"snippet-clipboard-content notranslate position-relative overflow-auto\" data-snippet-clipboard-copy-content=\" * Functional interface -- A special interfaces with \n * \t\texactly one abstract method can be used as lambda variable type.\n * \t\tAdditional methods if needed must not be abstract i.e. should have body\n * \t\tThe non-abstract methods must be declared either \u0026quot;default\u0026quot; or \u0026quot;static\u0026quot;\n * \tAnnotate the interface with \u0026quot;@FunctionalInterface\u0026quot; (from java.lang) to enforce compile time checking to ensure that there is exactly one abstract method.\n * \tThis is not needed by compiler to identify lambda interface, but it is a good practice to prevent accidental errors from other developers preventing the addition of other abstract methods.\n * Implementation of Functional Interface:\n * \t\tThe lone abstract method is used for lambda expression type, and lambda variable can eb used like an implementation class.\n * \t\tAny implementation class of the interface may choose to @override the non-abstract methods.\n * \t\tThe static method scan not be overriden but can be defined in the implementer and treated as a new method. \"\u003e\u003cpre class=\"notranslate\"\u003e\u003ccode\u003e * Functional interface -- A special interfaces with \n * \t\texactly one abstract method can be used as lambda variable type.\n * \t\tAdditional methods if needed must not be abstract i.e. should have body\n * \t\tThe non-abstract methods must be declared either \"default\" or \"static\"\n * \tAnnotate the interface with \"@FunctionalInterface\" (from java.lang) to enforce compile time checking to ensure that there is exactly one abstract method.\n * \tThis is not needed by compiler to identify lambda interface, but it is a good practice to prevent accidental errors from other developers preventing the addition of other abstract methods.\n * Implementation of Functional Interface:\n * \t\tThe lone abstract method is used for lambda expression type, and lambda variable can eb used like an implementation class.\n * \t\tAny implementation class of the interface may choose to @override the non-abstract methods.\n * \t\tThe static method scan not be overriden but can be defined in the implementer and treated as a new method. \n\u003c/code\u003e\u003c/pre\u003e\u003c/div\u003e\n\u003cdiv class=\"markdown-heading\" dir=\"auto\"\u003e\u003ch5 tabindex=\"-1\" class=\"heading-element\" dir=\"auto\"\u003eHow to execute a Lambda Expression\u003c/h5\u003e\u003ca id=\"user-content-how-to-execute-a-lambda-expression\" class=\"anchor\" aria-label=\"Permalink: How to execute a Lambda Expression\" href=\"#how-to-execute-a-lambda-expression\"\u003e\u003csvg class=\"octicon octicon-link\" viewBox=\"0 0 16 16\" version=\"1.1\" width=\"16\" height=\"16\" aria-hidden=\"true\"\u003e\u003cpath d=\"m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z\"\u003e\u003c/path\u003e\u003c/svg\u003e\u003c/a\u003e\u003c/div\u003e\n\u003cdiv class=\"snippet-clipboard-content notranslate position-relative overflow-auto\" data-snippet-clipboard-copy-content=\"* Call the interface method on the lambda variable, as if it were instance of a class.\n* ------------------------------------------------\n* Java compiler automatically infers the type from the method body - called \u0026quot;Type Inference\u0026quot;.\n* \tJava compiler can infer the return type as well as the parameter types by looking at the interface the lambda is implementing.\n* \tSo we can omit the parameter type as well, in the lambda expression.\n* * If there is only one parameter then we can omit the parenthesis surrounding the parameter of lambda function (RHS).\n* All 3 Below expressions are same:\n* \tStringLengthIF myLambda = (String s) -\u0026gt; {return s.length();};\n* \tStringLengthIF myLambda = (String s) -\u0026gt; s.length();\n* \tStringLengthIF myLambda = (s) -\u0026gt; s.length();\n* \n* Multiple ways to execute a lambda expression:\n* ------------------------\n* Hint: Use the lambda variable as if it were an implementation of the interface (lambda var type)\n* All of the below lines will result in the same.\n* \tgreeter.greet(lambdaGreeting);\n* \tlambdaGreeting.performAction(); // \u0026quot;performAction()\u0026quot; method name can be anything\n* \tgreeter.greet(() -\u0026gt; System.out.println(\u0026quot;Welcome Chinmay! - from lambda\u0026quot;)); // Using directly the body of the lambda func\n* Any method that takes the interface defined for lambda as argument can also take the lambda variable in the expression\"\u003e\u003cpre class=\"notranslate\"\u003e\u003ccode\u003e* Call the interface method on the lambda variable, as if it were instance of a class.\n* ------------------------------------------------\n* Java compiler automatically infers the type from the method body - called \"Type Inference\".\n* \tJava compiler can infer the return type as well as the parameter types by looking at the interface the lambda is implementing.\n* \tSo we can omit the parameter type as well, in the lambda expression.\n* * If there is only one parameter then we can omit the parenthesis surrounding the parameter of lambda function (RHS).\n* All 3 Below expressions are same:\n* \tStringLengthIF myLambda = (String s) -\u0026gt; {return s.length();};\n* \tStringLengthIF myLambda = (String s) -\u0026gt; s.length();\n* \tStringLengthIF myLambda = (s) -\u0026gt; s.length();\n* \n* Multiple ways to execute a lambda expression:\n* ------------------------\n* Hint: Use the lambda variable as if it were an implementation of the interface (lambda var type)\n* All of the below lines will result in the same.\n* \tgreeter.greet(lambdaGreeting);\n* \tlambdaGreeting.performAction(); // \"performAction()\" method name can be anything\n* \tgreeter.greet(() -\u0026gt; System.out.println(\"Welcome Chinmay! - from lambda\")); // Using directly the body of the lambda func\n* Any method that takes the interface defined for lambda as argument can also take the lambda variable in the expression\n\u003c/code\u003e\u003c/pre\u003e\u003c/div\u003e\n\u003cdiv class=\"markdown-heading\" dir=\"auto\"\u003e\u003ch5 tabindex=\"-1\" class=\"heading-element\" dir=\"auto\"\u003eHow to convert a normal function into an Lambda function Expression and how to execute it (with example)\u003c/h5\u003e\u003ca id=\"user-content-how-to-convert-a-normal-function-into-an-lambda-function-expression-and-how-to-execute-it-with-example\" class=\"anchor\" aria-label=\"Permalink: How to convert a normal function into an Lambda function Expression and how to execute it (with example)\" href=\"#how-to-convert-a-normal-function-into-an-lambda-function-expression-and-how-to-execute-it-with-example\"\u003e\u003csvg class=\"octicon octicon-link\" viewBox=\"0 0 16 16\" version=\"1.1\" width=\"16\" height=\"16\" aria-hidden=\"true\"\u003e\u003cpath d=\"m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z\"\u003e\u003c/path\u003e\u003c/svg\u003e\u003c/a\u003e\u003c/div\u003e\n\u003cdiv class=\"snippet-clipboard-content notranslate position-relative overflow-auto\" data-snippet-clipboard-copy-content=\" * HOW TO Convert a standard function into a Lambda Function\n * ----------------------------------------------------------- \n * \n * SUMMARY:\n * 1. First write the normal method (FN1).\n * 2. Define or Ensure that there is an \u0026quot;Functional Interface\u0026quot; (iFN1) defined matching with the function declaration (parameters and return type). The name of the interface or name of its parameters are immaterial.\n * 3. Now declare a variable \u0026quot;lambdaFN1\u0026quot; of type iFN1 (i.e. the new interface you defined) and assign the variable with the entire function body (FN1), similar to anonymous internal class.\n * 4. From the R.H.S of this expression remove function name, return type. And put an arrow (-\u0026gt;) between the parametes and function body. Terminate the assignment with a semicolon.\n * We are ready with the lambda expression.\n * We can use the lambda expression \u0026quot;lambdaFN1\u0026quot;, as if it is an implementation of interface \u0026quot;iFN1\u0026quot;\n * Also note that any functional interface (e.g. iFN1) can have only one abstract method, else it becoem a standard interface and can not be used with lambda expression.\n * \n * In \u0026quot;Lambda Expression we use a function as a value.\n * i.e. we assign a function body to a variable and will call that vaiable later to invoke the function assigned to it.\n * \tWhile assigning we \n * \t\ttrim out the func access specifier (public, private etc, are meaningful in a class, but not useful when the func is used standalone way), \n * \t\tremove the func name (func name is not needed, we will call the method by the assigned variable on left side of the lambda expression), \n * \t\tget rid of return type which is not neededas java compiler finds out the return type from the func body.\n * Finally add an arrow between the parameters and func body.\n * AND of the func body contains only one line, then we don't need a enclosing bracket.\n * AND if func body is one line and returns a value or expression, then we may omit \u0026quot;return\u0026quot; keyword.\n * \n * Now for Return Type of the lambda expression \n * \t\tDefine a random interface (say \u0026quot;MyLambda\u0026quot;) with a method declaration (with random name) with the signatures matching with the lambda function\n * we can use this interface as the variable type of the lambda expression.\n * i.e. Define an interface that describes the lambda expression matching params and return type. \n * Like any other expression, lambda expression too must end with a semicolon.\n * \n * Lambda functions are similar to Anonymous inner inline classes\n * e.g. GreetingIF = new GreetingIF(){\n * \t\tSystem.out.println(\u0026quot;Hello world from the anonymous ineer class inside Greeter main class.\u0026quot;);\n * \t };\n * \t //This 1. creates an implementation of interface GreetingIF, e. implements it's abstract method \n * \n * So there are BASICALLY 3 WAYS\n * A. Implement the interface and call the method from the interface variable which is implemented using \u0026quot;new\u0026quot; on the implementation class\n * B. Call the method from the lambda expression which indeirectly implements the interface\n * C. Call the method on the anonymous inner class that is just a variable of type interface instanciated through \u0026quot;new\u0026quot; on our interface, but implements the abstract method\n * \n * B \u0026amp; C are similar Lambda expression is just a fancy new shortcut in Java8 to represent anonymous inner classes.\n * But there are some difference. \n * \"\u003e\u003cpre class=\"notranslate\"\u003e\u003ccode\u003e * HOW TO Convert a standard function into a Lambda Function\n * ----------------------------------------------------------- \n * \n * SUMMARY:\n * 1. First write the normal method (FN1).\n * 2. Define or Ensure that there is an \"Functional Interface\" (iFN1) defined matching with the function declaration (parameters and return type). The name of the interface or name of its parameters are immaterial.\n * 3. Now declare a variable \"lambdaFN1\" of type iFN1 (i.e. the new interface you defined) and assign the variable with the entire function body (FN1), similar to anonymous internal class.\n * 4. From the R.H.S of this expression remove function name, return type. And put an arrow (-\u0026gt;) between the parametes and function body. Terminate the assignment with a semicolon.\n * We are ready with the lambda expression.\n * We can use the lambda expression \"lambdaFN1\", as if it is an implementation of interface \"iFN1\"\n * Also note that any functional interface (e.g. iFN1) can have only one abstract method, else it becoem a standard interface and can not be used with lambda expression.\n * \n * In \"Lambda Expression we use a function as a value.\n * i.e. we assign a function body to a variable and will call that vaiable later to invoke the function assigned to it.\n * \tWhile assigning we \n * \t\ttrim out the func access specifier (public, private etc, are meaningful in a class, but not useful when the func is used standalone way), \n * \t\tremove the func name (func name is not needed, we will call the method by the assigned variable on left side of the lambda expression), \n * \t\tget rid of return type which is not neededas java compiler finds out the return type from the func body.\n * Finally add an arrow between the parameters and func body.\n * AND of the func body contains only one line, then we don't need a enclosing bracket.\n * AND if func body is one line and returns a value or expression, then we may omit \"return\" keyword.\n * \n * Now for Return Type of the lambda expression \n * \t\tDefine a random interface (say \"MyLambda\") with a method declaration (with random name) with the signatures matching with the lambda function\n * we can use this interface as the variable type of the lambda expression.\n * i.e. Define an interface that describes the lambda expression matching params and return type. \n * Like any other expression, lambda expression too must end with a semicolon.\n * \n * Lambda functions are similar to Anonymous inner inline classes\n * e.g. GreetingIF = new GreetingIF(){\n * \t\tSystem.out.println(\"Hello world from the anonymous ineer class inside Greeter main class.\");\n * \t };\n * \t //This 1. creates an implementation of interface GreetingIF, e. implements it's abstract method \n * \n * So there are BASICALLY 3 WAYS\n * A. Implement the interface and call the method from the interface variable which is implemented using \"new\" on the implementation class\n * B. Call the method from the lambda expression which indeirectly implements the interface\n * C. Call the method on the anonymous inner class that is just a variable of type interface instanciated through \"new\" on our interface, but implements the abstract method\n * \n * B \u0026amp; C are similar Lambda expression is just a fancy new shortcut in Java8 to represent anonymous inner classes.\n * But there are some difference. \n * \n\u003c/code\u003e\u003c/pre\u003e\u003c/div\u003e\n\u003c/article\u003e","renderedFileInfo":null,"shortPath":null,"symbolsEnabled":true,"tabSize":8,"topBannersInfo":{"overridingGlobalFundingFile":false,"globalPreferredFundingPath":null,"showInvalidCitationWarning":false,"citationHelpUrl":"https://docs.github.com/github/creating-cloning-and-archiving-repositories/creating-a-repository-on-github/about-citation-files","actionsOnboardingTip":null},"truncated":false,"viewable":true,"workflowRedirectUrl":null,"symbols":null},"copilotInfo":null,"copilotAccessAllowed":false,"modelsAccessAllowed":false,"modelsRepoIntegrationEnabled":false,"csrf_tokens":{"/chinmay-anand/java8-experiment/branches":{"post":"aZqoY5WDQpWKd9UGq1TA6WDpFySHQt0uCHcmVGCPZwMvXGUMi9u2vMFXh7FcncIozyi9K1mK2hT5FjelK8bAGw"},"/repos/preferences":{"post":"gK7xy_8xn0lncvEVo4yH88uBLHtSDvZc2Ua1Tlp61hBWc-sHPZ-pm1WG_lKSwp5PhJ2zBKd-NkgvD4N-d5Pu9A"}}},"title":"java8-experiment/README.md at main · chinmay-anand/java8-experiment","appPayload":{"helpUrl":"https://docs.github.com","findFileWorkerPath":"/assets-cdn/worker/find-file-worker-263cab1760dd.js","findInFileWorkerPath":"/assets-cdn/worker/find-in-file-worker-1b17b3e7786a.js","githubDevUrl":null,"enabled_features":{"code_nav_ui_events":false,"react_blob_overlay":false,"accessible_code_button":true}}}
0