{"id":1810,"date":"2023-01-24T08:53:47","date_gmt":"2023-01-24T08:53:47","guid":{"rendered":"https:\/\/www.juniorjavadeveloper.pl\/?p=1810"},"modified":"2024-06-13T18:48:48","modified_gmt":"2024-06-13T18:48:48","slug":"java-wyrazenia-lambda-dla-kazdego-kod-java-krok-po-kroku","status":"publish","type":"post","link":"https:\/\/www.juniorjavadeveloper.pl\/en\/2023\/01\/24\/java-wyrazenia-lambda-dla-kazdego-kod-java-krok-po-kroku\/","title":{"rendered":"Java, Lambda expressions for everyone - step-by-step Java code"},"content":{"rendered":"<p class=\"has-large-font-size wp-block-paragraph\">In this article I will try to explain the issue of Lambda expressions available in Java. Lambda expressions shorten the code, making it more readable. This may seem like a small advantage and all the hype surrounding this element of the Java language is exaggerated. Fortunately, the improvement in code readability is very large. In short, instead of creating a new Java class (new file) that consists of eight lines of code, we can create a one-line Lambda expression that will do the same as a separate class, plus we have all the code in one place - no need to navigate after classes. Lambda expressions show their full potential when combined with Java Streams.<\/p>\n\n\n\n<p class=\"has-large-font-size wp-block-paragraph translation-block\">I will divide the article into two sections, each section will consist of three steps showing the transition and transformation of the code into Lambda expressions. Finally, I present a practical application of Lambda expressions with Java Streams - <a href=\"https:\/\/www.oracle.com\/technical-resources\/articles\/java\/ma14-java-se-8-streams.html\" target=\" _blank\" rel=\"noreferrer noopener\">Processing Data with Java SE 8 Streams<\/a>. When we already know the Lambda expressions, we do not have to go through the entire process, step by step, the last form from point <a href=\"#section-1-ad-3-anonymous-implementation-of-interface-method\" data-type=\" internal\" data-id=\"#section-1-ad-3-anonymous-implementation-of-an-interface-method\" target=\"_self\">Simple Lambda expression - anonymous implementation of an interface method<\/a>. Creating Lambda expressions in the steps described in this article are for educational purposes.<\/p>\n\n\n    <style>\r\n        .wpj-jtoc.--jtoc-theme-basic-light.--jtoc-has-custom-styles {\r\n            --jtoc-numeration-suffix: \". \";\n--jtoc-numeration-color: #adadad;\n        }\r\n\r\n            <\/style>\r\n\r\n\r\n\r\n<div id=\"wpj-jtoc\" class=\"wpj-jtoc wpj-jtoc--main --jtoc-the-content --jtoc-theme-basic-light --jtoc-title-align-left --jtoc-toggle-icon --jtoc-toggle-position-right --jtoc-toggle-1 --jtoc-has-numeration --jtoc-has-custom-styles --jtoc-is-unfolded\" >\r\n\r\n    \r\n    <!-- TOC -->\r\n    <div class=\"wpj-jtoc--toc\">\r\n                                <div class=\"wpj-jtoc--header\">\r\n                <div class=\"wpj-jtoc--header-main\">\r\n                                        <div class=\"wpj-jtoc--title\">\r\n                        <span class=\"wpj-jtoc--title-label\">Table of Contents<\/span>\r\n                    <\/div>\r\n                                                                <div class=\"wpj-jtoc--toggle-wrap\">\r\n                                                                                                                    <div class=\"wpj-jtoc--toggle-box\">\r\n                                    <div class=\"wpj-jtoc--toggle\"><\/div>\r\n                                <\/div>\r\n                                                    <\/div>\r\n                                    <\/div>\r\n            <\/div>\r\n                            <div class=\"wpj-jtoc--body\">\r\n                        <nav class=\"wpj-jtoc--nav\">\r\n                <ol class=\"wpj-jtoc--items\"><li class=\"wpj-jtoc--item --jtoc-h2\">\r\n        <div class=\"wpj-jtoc--item-content\" data-depth=\"2\">\r\n                <a href=\"#sekcja-1-prosta-metoda-dla-wyrazen-lambda\" title=\"Section #1 \u2013 a simple method for Lambda expressions\" data-numeration=\"1\">Section #1 \u2013 a simple method for Lambda expressions<\/a>\r\n                    <\/div> <ol class=\"wpj-jtoc--items\"><li class=\"wpj-jtoc--item --jtoc-h3\">\r\n        <div class=\"wpj-jtoc--item-content\" data-depth=\"3\">\r\n                <a href=\"#sekcja-1-ad-1-implementacja-interfejsu-oddzielna-klasa\" title=\"Classic implementation of the interface using a separate class.\" data-numeration=\"1.1\">Classic implementation of the interface using a separate class.<\/a>\r\n                    <\/div> <\/li><li class=\"wpj-jtoc--item --jtoc-h3\">\r\n        <div class=\"wpj-jtoc--item-content\" data-depth=\"3\">\r\n                <a href=\"#sekcja-1-ad-2-anonimowa-implementacja-interfejsu-anonimowa-klasa\" title=\"Anonymous implementation of an interface using an anonymous class\" data-numeration=\"1.2\">Anonymous implementation of an interface using an anonymous class<\/a>\r\n                    <\/div> <\/li><li class=\"wpj-jtoc--item --jtoc-h3\">\r\n        <div class=\"wpj-jtoc--item-content\" data-depth=\"3\">\r\n                <a href=\"#sekcja-1-ad-3-anonimowa-implementacja-metody-interfejsu\" title=\"Simple Lambda expression \u2013 anonymous implementation of an interface method\" data-numeration=\"1.3\">Simple Lambda expression \u2013 anonymous implementation of an interface method<\/a>\r\n                    <\/div> <\/li><\/ol><\/li><li class=\"wpj-jtoc--item --jtoc-h2\">\r\n        <div class=\"wpj-jtoc--item-content\" data-depth=\"2\">\r\n                <a href=\"#sekcja-2-metoda-zlozona-dla-wyrazen-lambda\" title=\"Section #2 \u2013 Complex Method for Lambda Expressions\" data-numeration=\"2\">Section #2 \u2013 Complex Method for Lambda Expressions<\/a>\r\n                    <\/div> <\/li><li class=\"wpj-jtoc--item --jtoc-h2\">\r\n        <div class=\"wpj-jtoc--item-content\" data-depth=\"2\">\r\n                <a href=\"#praktyczne-zastosowanie-wyrazen-lambda-java-streams\" title=\"Practical application of Lambda expressions from Java Streams\" data-numeration=\"3\">Practical application of Lambda expressions from Java Streams<\/a>\r\n                    <\/div> <\/li><\/ol>            <\/nav>\r\n                    <\/div>\r\n    <\/div>\r\n<\/div>\r\n\n\n\n<p class=\"has-large-font-size wp-block-paragraph\">Instead of creating a new Java class (new file) that consists of eight lines of code (plus a class with a main() method to run the example) ...<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">public class RetroJukebox implements Jukebox {\n    @Override\n    public void makeNoise() {\n        System.out.println(\"Retro Make Some Noise!\");\n    }\n}\n\npublic class LambdaStepByStepMain {\n    public static void main(String[] args) {\n        Jukebox retroJukebox = new RetroJukebox();\n        retroJukebox.makeNoise();\n    }\n}<\/pre>\n\n\n\n<p class=\"has-large-font-size wp-block-paragraph\">&hellip; mo&#380;emy utworzy&#263; jedno linijkowe wyra&#380;enie Lambda (plus klasa z metod&#261; main(), do uruchomienia przyk&#322;adu).<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">public class LambdaStepByStepMain {\n    public static void main(String[] args) {\n        Jukebox lambdaJukebox = \n            () -&gt; System.out.println(\"Lambda Make Some Noise!\");\n        lambdaJukebox.makeNoise();\n    }\n}<\/pre>\n\n\n\n<p class=\"has-large-font-size wp-block-paragraph\">Creating Lambda expressions is done using a functional interface. Yes, it is an interface well-known from Java. A functional interface must have one and only one method, which must be public and abstract. By default, methods in interfaces are public and abstract, hence the lack of keywords next to the method \u2013 public abstract void makeNoise().<\/p>\n\n\n\n<p class=\"has-large-font-size wp-block-paragraph\">Wi&#281;cej informacji o interfejsach mo&#380;na znale&#378;&#263; tutaj <a href=\"https:\/\/docs.oracle.com\/javase\/tutorial\/java\/IandI\/createinterface.html\" data-type=\"URL\" data-id=\"https:\/\/docs.oracle.com\/javase\/tutorial\/java\/IandI\/createinterface.html\" target=\"_blank\" rel=\"noreferrer noopener\">Interfaces (The Java&trade; Tutorials &gt; Learning the Java Language &gt; Interfaces and Inheritance)<\/a>, informacje o interfejsie funkcyjnym tutaj <a href=\"https:\/\/docs.oracle.com\/javase\/8\/docs\/api\/java\/lang\/FunctionalInterface.html\" data-type=\"URL\" data-id=\"https:\/\/docs.oracle.com\/javase\/8\/docs\/api\/java\/lang\/FunctionalInterface.html\" target=\"_blank\" rel=\"noreferrer noopener\">FunctionalInterface (Java Platform SE 8)<\/a>. Interfejs funkcyjny jest &ldquo;normalnym&rdquo; interfejsem Java, kt&oacute;ry ma pewne ograniczenia.<\/p>\n\n\n\n<p class=\"has-large-font-size wp-block-paragraph\">Oficjalna dokumentacja <a rel=\"noreferrer noopener\" href=\"https:\/\/docs.oracle.com\/javase\/tutorial\/java\/javaOO\/lambdaexpressions.html\" data-type=\"URL\" data-id=\"https:\/\/docs.oracle.com\/javase\/tutorial\/java\/javaOO\/lambdaexpressions.html\" target=\"_blank\">Oracle Java opisuje wyra&#380;ania Lambda<\/a> w nast&#281;puj&#261;cy spos&oacute;b &ldquo;Lambda expressions let you express instances of single-method classes more compactly.&rdquo;. Moja w&#322;asna interpretacja jest nast&#281;puj&#261;ca, &ldquo;lambda, to anonimowa implementacja metody&rdquo;.<\/p>\n\n\n\n<p class=\"ticss-38e1b35c has-large-font-size wp-block-paragraph translation-block\">For simplicity, I will present the code using the <code>main()<\/code> method. The right approach would be to create a unit test that verifies the correct operation of the written code. Below is the code with the <code>main()<\/code> method.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">public class LambdaStepByStepMain {\n    public static void main(String[] args) {\n        \/\/ TODO: Implementacja wyra&#380;enia Lambda, krok po kroku ...\n        \/\/ Sekcja #1 ...\n        \/\/ Sekcja #2 ...\n    }\n}<\/pre>\n\n\n\n<h3 class=\"wp-block-heading joli-heading jtoc-heading\" id=\"sekcja-1-prosta-metoda-dla-wyrazen-lambda\">Section #1 \u2013 a simple method for Lambda expressions<\/h3>\n\n\n\n<p class=\"has-large-font-size wp-block-paragraph translation-block\">For this example, I assume that a <em>simple method<\/em> is one that takes no parameters and returns nothing.<\/p>\n\n\n\n<p class=\"has-large-font-size wp-block-paragraph translation-block\">Below is the code of the <code>Jukebox<\/code> functional interface with the <code>void makeNoise()<\/code> method, which will be used to present Lambda expressions. As you can see, the <code>makeNoise()<\/code> method takes no parameters - empty brackets () - and does not return a value - the void keyword.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">@FunctionalInterface\npublic interface Jukebox {\n    void makeNoise();\n}<\/pre>\n\n\n\n<p class=\"has-large-font-size wp-block-paragraph\">Korzystanie z interfejsu, r&oacute;wnie&#380; funkcyjnego, wymaga jego implementacji. Poni&#380;ej zaprezentuj&#281; kilka sposob&oacute;w implementacji interfejsu. Przej&#347;cie do utworzenia wyra&#380;enia Lambda, czyli anonimowej implementacji metody, rozpoczn&#281; od kilku &ldquo;krok&oacute;w wstecz&rdquo;, tak, aby dok&#322;adnie wyja&#347;ni&#263;, dlaczego nazywam, to anonimow&#261; implementacj&#261; metody. <\/p>\n\n\n\n<p class=\"has-large-font-size wp-block-paragraph\">Steps to understand the process of moving to a Lambda expression:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li class=\"has-large-font-size\">Classic implementation of the interface using a separate class.<\/li>\n\n\n\n<li class=\"has-large-font-size\">Anonymous implementation of an interface using an anonymous class.<\/li>\n\n\n\n<li class=\"has-large-font-size\">Simple Lambda expression \u2013 anonymous implementation of an interface method<\/li>\n<\/ol>\n\n\n\n<p class=\"has-large-font-size wp-block-paragraph translation-block\">It's worth reading the official Oracle Java documentation - <a rel=\"noreferrer noopener\" href=\"https:\/\/docs.oracle.com\/javase\/tutorial\/java\/javaOO\/whentouse.html\" data-type=\"URL\" target=\"_blank\">When to Use Nested Classes, Local Classes, Anonymous Classes, and Lambda Expressions<\/a> to fully understand the steps I describe below.<\/p>\n\n\n\n<h4 class=\"wp-block-heading joli-heading jtoc-heading\" id=\"sekcja-1-ad-1-implementacja-interfejsu-oddzielna-klasa\">Classic implementation of the interface using a separate class.<\/h4>\n\n\n\n<p class=\"has-large-font-size wp-block-paragraph\">The first step to understanding Lambda expressions is described below, i.e. the classic implementation of the interface using a separate class.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">public class RetroJukebox implements Jukebox {\n    @Override\n    public void makeNoise() {\n        System.out.println(\"Retro Make Some Noise!\");\n    }\n}<\/pre>\n\n\n\n<p class=\"has-large-font-size wp-block-paragraph\">This way of implementing the interface and its methods requires creating an additional, separate class and a file with Java code. Then, you will need to create an object for the new class and call the appropriate method, as shown in the code below.<\/p>\n\n\n\n<div class=\"wp-block-columns alignwide are-vertically-aligned-center is-layout-flex wp-container-core-columns-is-layout-8f761849 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-vertically-aligned-center is-layout-flow wp-block-column-is-layout-flow\">\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">public class LambdaStepByStepMain {\n    public static void main(String[] args) {\n        Jukebox retroJukebox = new RetroJukebox();\n        retroJukebox.makeNoise();\n    }\n}<\/pre>\n<\/div>\n<\/div>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">Retro Make Some Noise!\n\nProcess finished with exit code 0<\/pre>\n\n\n\n<p class=\"has-large-font-size wp-block-paragraph\">The result of running the code from the main() method of the LambdaStepByStepMain class.<\/p>\n\n\n\n<p class=\"has-large-font-size wp-block-paragraph\">Jak wida&#263; powy&#380;sze rozwi&#261;zanie wymaga sporo kodu, kt&oacute;ry jest &ldquo;rozrzucony&rdquo; w dw&oacute;ch r&oacute;&#380;nych klasach, RetroJukeBox oraz LambdaStepByStepMain. Tak wygl&#261;da klasyczne podej&#347;cie do pisania kodu w j&#281;zyku Java. Wyra&#380;enia Lambda zmieniaj&#261;, to podej&#347;cie.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity is-style-default\">\n\n\n\n<h4 class=\"wp-block-heading joli-heading jtoc-heading\" id=\"sekcja-1-ad-2-anonimowa-implementacja-interfejsu-anonimowa-klasa\">Anonymous implementation of an interface using an anonymous class<\/h4>\n\n\n\n<p class=\"has-large-font-size wp-block-paragraph\">The second step to understanding Lambda expressions is described below, i.e. anonymous implementation of the interface using an anonymous class.<\/p>\n\n\n\n<div class=\"wp-block-columns alignwide are-vertically-aligned-center is-layout-flex wp-container-core-columns-is-layout-8f761849 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-vertically-aligned-center is-layout-flow wp-block-column-is-layout-flow\">\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">public class LambdaStepByStepMain {\n    public static void main(String[] args) {\n        Jukebox anonymousJukebox = new Jukebox() {\n            @Override\n            public void makeNoise() {\n                System.out.println(\"Anonymous Make Some Noise!\");\n            }\n        };\n        anonymousJukebox.makeNoise();\n    }\n}<\/pre>\n<\/div>\n<\/div>\n\n\n\n<p class=\"has-large-font-size wp-block-paragraph translation-block\">The above implementation method does not require a separate class, it is replaced by an anonymous interface implementation that uses the anonymous class with the implementation of the makeNoise() method and then creates an object for it - <em>Jukebox anonymousJukebox = new Jukebox() {}<\/em>. The appropriate method is called again.<\/p>\n\n\n\n<p class=\"has-large-font-size wp-block-paragraph\">This code snippet can be broken down into several elements to better understand it:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li class=\"has-large-font-size translation-block\">Declaration of the <strong>anonymous Jukebox<\/strong> variable, of type <strong>Jukebox<\/strong> (functional interface), to which we assign a value - fragment <strong>Jukebox anonymousJukebox =<\/strong>.<\/li>\n\n\n\n<li class=\"has-large-font-size translation-block\">An anonymous class that implements the <strong>makeNoise()<\/strong> method of the <strong>Jukebox<\/strong> interface - the following code fragment.<\/li>\n<\/ol>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">new Jukebox() {\n    @Override\n    public void makeNoise() {\n        System.out.println(\"Anonymous Make Some Noise!\");\n    }\n};<\/pre>\n\n\n\n<p class=\"has-large-font-size wp-block-paragraph translation-block\">More information about anonymous classes can be found here <a href=\"https:\/\/docs.oracle.com\/javase\/tutorial\/java\/javaOO\/anonymousclasses.html\" data-type=\"URL\" data-id=\"https:\/ \/docs.oracle.com\/javase\/tutorial\/java\/javaOO\/anonymousclasses.html\" target=\"_blank\" rel=\"noreferrer noopener\">Anonymous Classes (The Java\u2122 Tutorials &gt; Learning the Java Language &gt; Classes and Objects)<\/a>.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">Anonymous Make Some Noise!\n\nProcess finished with exit code 0<\/pre>\n\n\n\n<p class=\"has-large-font-size wp-block-paragraph\">The result of running the code from the main() method of the LambdaStepByStepMain class.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity is-style-default\">\n\n\n\n<h4 class=\"wp-block-heading joli-heading jtoc-heading\" id=\"sekcja-1-ad-3-anonimowa-implementacja-metody-interfejsu\">Proste wyra&#380;enie Lambda &ndash; anonimowa implementacja metody interfejsu<\/h4>\n\n\n\n<p class=\"has-large-font-size wp-block-paragraph\">Poni&#380;ej opisany jest trzeci krok do zrozumienia wyra&#380;e&#324; Lambda, kt&oacute;ry prezentuje wyra&#380;enia Lambda we w&#322;asnej osobie.<\/p>\n\n\n\n<div class=\"wp-block-columns alignwide are-vertically-aligned-center is-layout-flex wp-container-core-columns-is-layout-8f761849 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-vertically-aligned-center is-layout-flow wp-block-column-is-layout-flow\">\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">public class LambdaStepByStepMain {\n    public static void main(String[] args) {\n        Jukebox lambdaJukebox = \n            () -&gt; System.out.println(\"Lambda Make Some Noise!\");\n        lambdaJukebox.makeNoise();\n    }\n}<\/pre>\n<\/div>\n<\/div>\n\n\n\n<p class=\"has-large-font-size wp-block-paragraph\">Anonimowa implementacja metody z wykorzystaniem wyra&#380;enia Lambda &ndash; zamiast tworzy&#263; dodatkow&#261;, oddzieln&#261; klas&#281; (plik z kodem Java) jak w <a href=\"#sekcja-1-ad-1-implementacja-interfejsu-oddzielna-klasa\" data-type=\"internal\" data-id=\"#sekcja-1-ad-1-implementacja-interfejsu-oddzielna-klasa\">Ad. 1. Klasyczna implementacja interfejsu z wykorzystaniem oddzielnej klasy<\/a> lub anonimow&#261; klas&#281; implementuj&#261;c&#261; interfejs jak w <a href=\"#sekcja-1-ad-2-anonimowa-implementacja-interfejsu-anonimowa-klasa\" data-type=\"internal\" data-id=\"#sekcja-1-ad-2-anonimowa-implementacja-interfejsu-anonimowa-klasa\">Ad. 2. Anonimowa implementacja interfejsu z wykorzystaniem anonimowej klasy<\/a>.<\/p>\n\n\n\n<p class=\"has-large-font-size wp-block-paragraph\">Ten fragment kodu trzeba rozbi&#263; na kilka element&oacute;w, aby lepiej zrozumie&#263; wyra&#380;enia Lambda: <strong>Jukebox lambdaJukebox = () -&gt; System.out.println(&ldquo;Lambda Make Some Noise!&rdquo;);<\/strong><\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li class=\"has-large-font-size\">Deklaracja zmiennej <strong>lambdaJukebox<\/strong>, typu <strong>Jukebx<\/strong> (interfejs funkcyjny), do kt&oacute;rej przypisujemy warto&#347;&#263; &ndash; fragment <strong>Jukebox lambdaJukebox =<\/strong>.<\/li>\n\n\n\n<li class=\"has-large-font-size\">Anonimowa implementacja metody z wykorzystaniem wyra&#380;enia Lambda &ndash; fragment <strong>() -&gt; System.out.println(&ldquo;Lambda Make Some Noise!&rdquo;);<\/strong><\/li>\n<\/ol>\n\n\n\n<div class=\"wp-block-columns alignwide are-vertically-aligned-center is-layout-flex wp-container-core-columns-is-layout-8f761849 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-vertically-aligned-center is-layout-flow wp-block-column-is-layout-flow\">\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">@FunctionalInterface\npublic interface Jukebox {\n    void makeNoise();\n}\n\nJukebox lambdaJukebox = \n    () -&gt; System.out.println(\"Lambda Make Some Noise!\");<\/pre>\n<\/div>\n<\/div>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">Lambda Make Some Noise!\n\nProcess finished with exit code 0<\/pre>\n\n\n\n<p class=\"has-large-font-size wp-block-paragraph\">The result of running the code from the main() method of the LambdaStepByStepMain class.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity is-style-default\">\n\n\n\n<div class=\"wp-block-columns alignwide are-vertically-aligned-center is-layout-flex wp-container-core-columns-is-layout-8f761849 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-vertically-aligned-center is-layout-flow wp-block-column-is-layout-flow\">\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">public class LambdaStepByStepMain {\n    public static void main(String[] args) {\n        \/\/ Ad. 1. Klasyczna implementacja interfejsu z wykorzystaniem oddzielnej klasy\n        Jukebox retroJukebox = new RetroJukebox();\n        retroJukebox.makeNoise();\n\n        \/\/ Ad. 2. Anonimowa implementacja interfejsu z wykorzystaniem anonimowej klasy\n        Jukebox anonymousJukebox = new Jukebox() {\n            @Override\n            public void makeNoise() {\n                System.out.println(\"Anonymous Make Some Noise!\");\n            }\n        };\n        anonymousJukebox.makeNoise();\n\n        \/\/ Ad. 3. Proste wyra&#380;enie Lambda - anonimowa implementacja metody interfejsu\n        Jukebox lambdaJukebox = () -&gt; System.out.println(\"Lambda Make Some Noise!\");\n        lambdaJukebox.makeNoise();\n    }\n}<\/pre>\n<\/div>\n<\/div>\n\n\n\n<p class=\"has-large-font-size wp-block-paragraph\">Powy&#380;szy kod prezentuje wszystkie kroki w jednym miejscu, to ca&#322;y proces przej&#347;cia do wyra&#380;e&#324; Lambda w jednej metodzie <code>main()<\/code>. <\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">Retro Make Some Noise!\nAnonymous Make Some Noise!\nLambda Make Some Noise!\n\nProcess finished with exit code 0<\/pre>\n\n\n\n<p class=\"has-large-font-size wp-block-paragraph\">Wynik dzia&#322;ania kodu z metody main() z klasy LambdaStepByStepMain dla wszystkich krok&oacute;w prowadz&#261;cych do wyja&#347;nienia wyra&#380;enia Lambda &ndash; dla <a href=\"#sekcja-1-prosta-metoda-dla-wyrazen-lambda\">Section #1 \u2013 a simple method for Lambda expressions<\/a>.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\">\n\n\n\n<p class=\"has-large-font-size wp-block-paragraph\">Poni&#380;ej prezentuj&#281; w jednym miejscu wszystkie kroki. Jest, to ca&#322;y proces przej&#347;cia do wyra&#380;e&#324; Lambda. Oczywi&#347;cie pisz&#261;c kod wyra&#380;e&#324; Lambda nie musimy przechodzi&#263; przez ca&#322;y proces, krok po kroku, wystarczy ostatnia forma <a href=\"#sekcja-1-ad-3-anonimowa-implementacja-metody-interfejsu\">Ad. 3. Proste wyra&#380;enie Lambda &ndash; anonimowa implementacja metody interfejsu<\/a> Tworzenie wyra&#380;e&#324; Lambda w krokach opisane w tym artykule s&#261; w celach edukacyjnych.<\/p>\n\n\n\n<div class=\"wp-block-columns alignwide are-vertically-aligned-center is-layout-flex wp-container-core-columns-is-layout-8f761849 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-vertically-aligned-center is-layout-flow wp-block-column-is-layout-flow\">\n<p class=\"wp-block-paragraph\"><a href=\"#sekcja-1-ad-1-implementacja-interfejsu-oddzielna-klasa\">Ad. 1. Klasyczna implementacja interfejsu&hellip;<\/a><\/p>\n<\/div>\n\n\n\n<div class=\"wp-block-column is-vertically-aligned-center is-layout-flow wp-block-column-is-layout-flow\" style=\"flex-basis:70%\">\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">public class RetroJukebox implements Jukebox {\n    @Override\n    public void makeNoise() {\n        System.out.println(\"Retro Make Some Noise!\");\n    }\n}<\/pre>\n<\/div>\n<\/div>\n\n\n\n<div class=\"wp-block-columns alignwide are-vertically-aligned-center is-layout-flex wp-container-core-columns-is-layout-8f761849 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-vertically-aligned-center is-layout-flow wp-block-column-is-layout-flow\">\n<p class=\"wp-block-paragraph\"><a href=\"#sekcja-1-ad-2-anonimowa-implementacja-interfejsu-anonimowa-klasa\" data-type=\"internal\" data-id=\"#sekcja-1-ad-2-anonimowa-implementacja-interfejsu-anonimowa-klasa\">Ad. 2. Anonimowa implementacja interfejsu&hellip;<\/a><\/p>\n<\/div>\n\n\n\n<div class=\"wp-block-column is-vertically-aligned-center is-layout-flow wp-block-column-is-layout-flow\" style=\"flex-basis:70%\">\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">Jukebox anonymousJukebox = new Jukebox() {\n    @Override\n    public void makeNoise() {\n        System.out.println(\"Anonymous Make Some Noise!\");\n    }\n};<\/pre>\n<\/div>\n<\/div>\n\n\n\n<div class=\"wp-block-columns alignwide are-vertically-aligned-center is-layout-flex wp-container-core-columns-is-layout-8f761849 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-vertically-aligned-center is-layout-flow wp-block-column-is-layout-flow\">\n<p class=\"wp-block-paragraph\"><a href=\"#sekcja-1-ad-3-anonimowa-implementacja-metody-interfejsu\">Ad. 3. Proste wyra&#380;enie Lambda&hellip;<\/a><\/p>\n<\/div>\n\n\n\n<div class=\"wp-block-column is-vertically-aligned-center is-layout-flow wp-block-column-is-layout-flow\" style=\"flex-basis:70%\">\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">Jukebox lambdaJukebox = \n    () -&gt; System.out.println(\"Lambda Make Some Noise!\");<\/pre>\n<\/div>\n<\/div>\n\n\n\n<p class=\"has-large-font-size wp-block-paragraph\">W powy&#380;szym kodzie wida&#263;, jak dla metody <code>public void makeNoise()<\/code> &ldquo;znikaj&#261; zb&#281;dne elementy&rdquo;, kt&oacute;re kompilator Java dobrze zna, bo opisali&#347;my je w interfejsie <code>Jukebox<\/code>. Co rozumiem przez znikaj&#261;ce zb&#281;dne elementy?<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li class=\"has-large-font-size\">Zb&#281;dne elementy dla wyra&#380;enia Lambda, to: sama deklaracja metody <code>public void makeNoise<\/code> oraz cia&#322;o metody zawarte mi&#281;dzy nawiasami klamrowymi <code>{System.out.println(\"Anonymous Make Some Noise!\");}<\/code>. <\/li>\n\n\n\n<li class=\"has-large-font-size\">Niezb&#281;dne elementy dla wyra&#380;ania Lambda, to: parametry metody zawarte mi&#281;dzy nawiasami <code>()<\/code> oraz cia&#322;o metody umieszczone po &ldquo;strza&#322;ce&rdquo; <code>-&gt; System.out.println(\"Anonymous Make Some Noise!\");<\/code>.<\/li>\n<\/ul>\n\n\n\n<div class=\"wp-block-group alignwide is-vertical is-content-justification-center is-layout-flex wp-container-core-group-is-layout-524f8de7 wp-block-group-is-layout-flex\">\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\"> public void makeNoise() {\n    System.out.println(\"Anonymous Make Some Noise!\");\n }<\/pre>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">() -&gt; System.out.println(\"Lambda Make Some Noise!\"); <\/pre>\n<\/div>\n\n\n\n<p class=\"has-large-font-size wp-block-paragraph\">Wi&#281;cej informacji o metodach mo&#380;na znale&#378;&#263; tutaj <a rel=\"noreferrer noopener\" href=\"https:\/\/docs.oracle.com\/javase\/tutorial\/java\/javaOO\/methods.html\" data-type=\"URL\" data-id=\"https:\/\/docs.oracle.com\/javase\/tutorial\/java\/javaOO\/methods.html\" target=\"_blank\">Defining Methods (The Java&trade; Tutorials &gt; Learning the Java Language &gt; Classes and Objects)<\/a>.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\">\n\n\n\n<h3 class=\"wp-block-heading joli-heading jtoc-heading\" id=\"sekcja-2-metoda-zlozona-dla-wyrazen-lambda\">Sekcja #2 &ndash; metoda z&#322;o&#380;ona dla wyra&#380;e&#324; Lambda<\/h3>\n\n\n\n<p class=\"has-large-font-size wp-block-paragraph\">Dla tego przyk&#322;adu przyjmuj&#281;, &#380;e <em>metoda z&#322;o&#380;ona<\/em>, to taka, kt&oacute;ra przyjmuje parametr oraz zwraca warto&#347;&#263;.<\/p>\n\n\n\n<p class=\"has-large-font-size wp-block-paragraph\">Poni&#380;ej kod interfejsu funkcyjnego <code>Printer<\/code> z metod&#261; <code>String printText(String text)<\/code>, kt&oacute;ry b&#281;dzie u&#380;ywany do zaprezentowania wyra&#380;e&#324; Lambda. Jak wida&#263; metoda przyjmuje parametr &lsquo;text&rsquo; oraz zwraca warto&#347;&#263; typu &lsquo;String&rsquo;.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">@FunctionalInterface\npublic interface Printer {\n    String printText(String text);\n}<\/pre>\n\n\n\n<p class=\"has-large-font-size wp-block-paragraph\">Posiadaj&#261;c wiedz&#281; z sekcji #1 pozwol&#281; sobie zaprezentowa&#263; tylko ca&#322;y proces przej&#347;cia do wyra&#380;e&#324; Lambda, zawieraj&#261;cy wszystkie kroki w jednym miejscu.<\/p>\n\n\n\n<div class=\"wp-block-columns alignwide are-vertically-aligned-center is-layout-flex wp-container-core-columns-is-layout-8f761849 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-vertically-aligned-center is-layout-flow wp-block-column-is-layout-flow\">\n<p class=\"wp-block-paragraph\"><a href=\"https:\/\/www.juniorjavadeveloper.pl\/wp-admin\/post.php?post=1810&amp;action=edit#sekcja-1-ad-1-implementacja-interfejsu-oddzielna-klasa\">Ad. 1. Klasyczna implementacja interfejsu&hellip;<\/a><\/p>\n<\/div>\n\n\n\n<div class=\"wp-block-column is-vertically-aligned-center is-layout-flow wp-block-column-is-layout-flow\" style=\"flex-basis:70%\">\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">public class LocalPrinter implements Printer{\n    @Override\n    public String printText(String text) {\n        return \"Local printer is printing: \" + text;\n    }\n}<\/pre>\n<\/div>\n<\/div>\n\n\n\n<div class=\"wp-block-columns alignwide are-vertically-aligned-center is-layout-flex wp-container-core-columns-is-layout-8f761849 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-vertically-aligned-center is-layout-flow wp-block-column-is-layout-flow\">\n<p class=\"wp-block-paragraph\"><a href=\"#sekcja-1-ad-2-anonimowa-implementacja-interfejsu-anonimowa-klasa\" data-type=\"internal\" data-id=\"#sekcja-1-ad-2-anonimowa-implementacja-interfejsu-anonimowa-klasa\">Ad. 2. Anonimowa implementacja interfejsu&hellip;<\/a><\/p>\n<\/div>\n\n\n\n<div class=\"wp-block-column is-vertically-aligned-center is-layout-flow wp-block-column-is-layout-flow\" style=\"flex-basis:70%\">\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">Printer anonymousPrinter = new Printer() {\n    @Override\n    public String printText(String text) {\n        return \"Anonymous printer is printing: \" + text;\n    }\n};<\/pre>\n<\/div>\n<\/div>\n\n\n\n<div class=\"wp-block-columns alignwide are-vertically-aligned-center is-layout-flex wp-container-core-columns-is-layout-8f761849 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-vertically-aligned-center is-layout-flow wp-block-column-is-layout-flow\">\n<p class=\"wp-block-paragraph\"><a href=\"#sekcja-1-ad-3-anonimowa-implementacja-metody-interfejsu\">Ad. 3. Proste wyra&#380;enie Lambda&hellip;<\/a><\/p>\n<\/div>\n\n\n\n<div class=\"wp-block-column is-vertically-aligned-center is-layout-flow wp-block-column-is-layout-flow\" style=\"flex-basis:70%\">\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\/\/ Lambda - pe&#322;na wersja zapisu.\nPrinter fullLambdaPrinter = (text) -&gt; {\n    return \"Printing some text: \" + text;\n};<\/pre>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\/\/ Lambda - skr&oacute;cona wersja zapisu.\nPrinter shortLambdaPrinter = \n    text -&gt; \"Printing some text: \" + text;<\/pre>\n<\/div>\n<\/div>\n\n\n\n<p class=\"has-large-font-size wp-block-paragraph\">Wynik dzia&#322;ania kodu z metody main() z klasy LambdaStepByStepMain dla wszystkich krok&oacute;w prowadz&#261;cych do wyja&#347;nienia wyra&#380;enia Lambda &ndash; dla <a href=\"#sekcja-2-metoda-zlozona-dla-wyrazen-lambda\">Sekcja #2 &ndash; metoda z&#322;o&#380;ona dla wyra&#380;e&#324; Lambda<\/a>.<\/p>\n\n\n\n<div class=\"wp-block-columns alignwide are-vertically-aligned-center is-layout-flex wp-container-core-columns-is-layout-8f761849 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-vertically-aligned-center is-layout-flow wp-block-column-is-layout-flow\">\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">public class LambdaStepByStepMain {\n    public static void main(String[] args) {\n        \/\/ Sekcja #2\n\n        \/\/ #1 Separate Class for Interface method 'makeNoise()' implementation.\n        Printer homePrinter = new LocalPrinter();\n        String homePrinterText = homePrinter.printText(\"Local Home Printer example\");\n        System.out.println(homePrinterText);\n\n\n        \/\/ #2 Anonymous Interface implementation with method 'printText()'.\n        Printer anonymousPrinter = new Printer() {\n            @Override\n            public String printText(String text) {\n                return \"Anonymous printer is printing: \" + text;\n            }\n        };\n        String anonymousPrinterText = \n            anonymousPrinter.printText(\"Anonymous Printer example\");\n        System.out.println(anonymousPrinterText);\n\n        \/\/ #3 Lambda Anonymous method 'printText()' Implementation.\n        \/\/ Lambda expression with input parameter and return value.\n        \/\/ Lambda full version.\n        Printer fullLambdaPrinter = (text) -&gt; {\n            return \"Printing some text: \" + text;\n        };\n        String fullLambdaText = fullLambdaPrinter.printText(\"Full Lambda example\");\n        System.out.println(fullLambdaText);\n\n        \/\/ Lambda expression with input parameter and return value.\n        \/\/ Lambda short version.\n        Printer shortLambdaPrinter = text -&gt; \"Printing some text: \" + text;\n        String shortLambdaText = shortLambdaPrinter.printText(\"Short Lambda example\");\n        System.out.println(shortLambdaText);\n}<\/pre>\n<\/div>\n<\/div>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">Local printer is printing: Local Home Printer example\nAnonymous printer is printing: Anonymous Printer example\nPrinting some text: Full Lambda example\nPrinting some text: Short Lambda example\n\nProcess finished with exit code 0<\/pre>\n\n\n\n<p class=\"has-large-font-size wp-block-paragraph\">Wynik dzia&#322;ania kodu z metody main() z klasy LambdaStepByStepMain dla wszystkich krok&oacute;w prowadz&#261;cych do wyja&#347;nienia wyra&#380;enia Lambda &ndash; dla sekcji #2.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\">\n\n\n\n<h3 class=\"wp-block-heading joli-heading jtoc-heading\" id=\"praktyczne-zastosowanie-wyrazen-lambda-java-streams\">Praktyczne zastosowanie wyra&#380;e&#324; Lambda z Java Streams<\/h3>\n\n\n\n<p class=\"has-large-font-size wp-block-paragraph\">Poni&#380;ej zaprezentuj&#281; niewielki przyk&#322;ad praktycznego wykorzystania wyra&#380;e&#324; Lambda w Java Streams, pomimo, &#380;e w tym artykule nie opisz&#281; szczeg&oacute;&#322;&oacute;w zwi&#261;zanych z Java Streams. Poni&#380;szy przyk&#322;ad poka&#380;e moc wyra&#380;e&#324; Lambda, kt&oacute;ra prowadzi do przejrzystego i eleganckiego kodu, kt&oacute;rego logika dzia&#322;ania jest dost&#281;pna od razu w jednym miejscu, co prowadzi do poprawy czytelno&#347;ci kodu.<\/p>\n\n\n\n<div class=\"wp-block-columns alignwide are-vertically-aligned-center is-layout-flex wp-container-core-columns-is-layout-8f761849 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-vertically-aligned-center is-layout-flow wp-block-column-is-layout-flow\">\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">public class LambdaStreamsMain {\n    public static void main(String[] args) {\n        List&lt;Person&gt; people = Arrays.asList(\n                new Person(\"John\", 29, Person.Gender.MALE),\n                new Person(\"Christina\", 19, Person.Gender.FEMALE),\n                new Person(\"Max\", 33, Person.Gender.MALE)\n        );\n\n        \/\/ Using Lambda in short form ...\n        List&lt;String&gt; collectedPeopleNamesShortLambda = people.stream()\n                .filter(person -&gt; person.getGender().equals(Person.Gender.MALE))\n                .map(Person::getName)\n                .collect(Collectors.toList());\n\n        System.out.println(\"All male names (short Lambda): \" +\n            collectedPeopleNamesShortLambda);\n\n        \/\/ Using Lambda in full form ...\n        List&lt;String&gt; collectedPeopleNamesFullLambda = people.stream()\n                .filter((person) -&gt; person.getGender().equals(Person.Gender.MALE))\n                .map((person) -&gt; person.getName())\n                .collect(Collectors.toList());\n\n        System.out.println(\"All male names (full Lambda): \" +\n            collectedPeopleNamesFullLambda);\n}<\/pre>\n<\/div>\n<\/div>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">All male names: [John, Max]\n\nProcess finished with exit code 0<\/pre>\n\n\n\n<p class=\"has-large-font-size wp-block-paragraph\">Powy&#380;ej wynik dzia&#322;ania kodu z metody main() z klasy LambdaStreamsMain pokazuj&#261;cej u&#380;ycie wyra&#380;e&#324; Lambda dla Java Streams.<\/p>\n\n\n\n<p class=\"has-large-font-size wp-block-paragraph\">Poni&#380;ej kod z czas&oacute;w Java 1.5 (od grudnia 2006 r.), kt&oacute;ry nie posiada&#322; wyra&#380;e&#324; Lambda oraz Java Streams &hellip;<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">List&lt;String&gt; peopleMaleNames = new ArrayList&lt;&gt;();\nfor (Person person : people) {\n    if (person.getGender().equals(Person.Gender.MALE)) {\n        peopleMaleNames.add(person.getName());\n    }\n}<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">&hellip; po o&#347;miu latach w Java 1.8 (od marca 2014 r.) wprowadzono wyra&#380;enia Lambda &ndash; <a rel=\"noreferrer noopener\" href=\"https:\/\/en.wikipedia.org\/wiki\/Java_version_history\" data-type=\"URL\" data-id=\"https:\/\/en.wikipedia.org\/wiki\/Java_version_history\" target=\"_blank\">Wikipedia: Java version history<\/a>.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">List&lt;String&gt; collectedPeopleNamesShortLambda = people.stream()\n    .filter(person -&gt;\n        person.getGender().equals(Person.Gender.MALE))\n    .map(Person::getName)\n    .collect(Collectors.toList());<\/pre>\n\n\n\n<p class=\"has-large-font-size wp-block-paragraph\">Podsumowuj&#261;c powy&#380;sze dwie sekcje oraz kroki w nich zawarte, wida&#263; wyra&#378;nie, &#380;e wyra&#380;enie Lambda jest bardzo kr&oacute;tkie i zwi&#281;z&#322;e, a dodatkowo kod implementacji samej metody makeNoise() jest od razu widoczny. To, &#380;e wyra&#380;enia Lambda s&#261; kr&oacute;tkie i zwi&#281;z&#322;e sprawia, &#380;e s&#261; bardzo u&#380;yteczne w Java Streams, kt&oacute;re operuj&#261; na Java Collections.<\/p>\n\n\n\n<figure class=\"wp-block-embed is-type-wp-embed is-provider-poradnik-dla-przysz-ych-junior-java-developer-039-w wp-block-embed-poradnik-dla-przysz-ych-junior-java-developer-039-w\"><div class=\"wp-block-embed__wrapper\">\n<blockquote class=\"wp-embedded-content\" data-secret=\"WSmiwJefkP\"><a href=\"https:\/\/www.juniorjavadeveloper.pl\/en\/kontakt\/\">Contact<\/a><\/blockquote><iframe loading=\"lazy\" class=\"wp-embedded-content\" sandbox=\"allow-scripts\" security=\"restricted\" style=\"position: absolute; visibility: hidden;\" title=\"&bdquo;Kontakt&rdquo; &mdash; Poradnik dla przysz&#322;ych Junior Java Developer&amp;apos;&oacute;w\" src=\"https:\/\/www.juniorjavadeveloper.pl\/kontakt\/embed\/#?secret=cMLV5PK175#?secret=WSmiwJefkP\" data-secret=\"WSmiwJefkP\" width=\"600\" height=\"338\" frameborder=\"0\" marginwidth=\"0\" marginheight=\"0\" scrolling=\"no\"><\/iframe>\n<\/div><\/figure>\n\n\n\n<figure class=\"wp-block-embed is-type-wp-embed is-provider-poradnik-dla-przysz-ych-junior-java-developer-039-w wp-block-embed-poradnik-dla-przysz-ych-junior-java-developer-039-w\"><div class=\"wp-block-embed__wrapper\">\n<blockquote class=\"wp-embedded-content\" data-secret=\"GfzhetkQBn\"><a href=\"https:\/\/www.juniorjavadeveloper.pl\/en\/mentoring-rezerwuj-termin\/\">Mentoring - book an appointment<\/a><\/blockquote><iframe loading=\"lazy\" class=\"wp-embedded-content\" sandbox=\"allow-scripts\" security=\"restricted\" style=\"position: absolute; visibility: hidden;\" title=\"&bdquo;Mentoring &ndash; rezerwuj termin&rdquo; &mdash; Poradnik dla przysz&#322;ych Junior Java Developer&amp;apos;&oacute;w\" src=\"https:\/\/www.juniorjavadeveloper.pl\/mentoring-rezerwuj-termin\/embed\/#?secret=AIeegd5IK3#?secret=GfzhetkQBn\" data-secret=\"GfzhetkQBn\" width=\"600\" height=\"338\" frameborder=\"0\" marginwidth=\"0\" marginheight=\"0\" scrolling=\"no\"><\/iframe>\n<\/div><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">Zdj&#281;cie autorstwa <a href=\"https:\/\/www.pexels.com\/pl-pl\/zdjecie\/ludzie-idacy-po-schodach-3685201\/\" data-type=\"URL\" data-id=\"https:\/\/www.pexels.com\/pl-pl\/zdjecie\/ludzie-idacy-po-schodach-3685201\/\" target=\"_blank\" rel=\"noreferrer noopener\">Mitchell Luo<\/a> z <a rel=\"noreferrer noopener\" href=\"https:\/\/www.pexels.com\/pl-pl\/\" target=\"_blank\">Pexels<\/a>.<\/p>","protected":false},"excerpt":{"rendered":"<p>W tym artykule postaram si\u0119 przybli\u017cy\u0107 zagadnienie wyra\u017ce\u0144 Lambda dost\u0119pnych w j\u0119zyku Java. Wyra\u017cenia Lambda skracaj\u0105 zapis kodu, tym samym czyni\u0105c go bardziej czytelnym. Mo\u017ce si\u0119 to wydawa\u0107 niewielk\u0105 zalet\u0105 i ca\u0142y szum w oko\u0142o, tego elementu j\u0119zyka Java jest na wyrost. Na szcz\u0119\u015bcie poprawa czytelno\u015bci kodu jest bardzo du\u017ca. W telegraficznym skr\u00f3cie, zamiast tworzy\u0107 [&hellip;]<\/p>","protected":false},"author":1,"featured_media":2431,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_ppp_document_settings_meta":"","footnotes":"","jetpack_publicize_message":"","jetpack_is_tweetstorm":false,"jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","enabled":false}}},"categories":[3,7],"tags":[],"class_list":["post-1810","post","type-post","status-publish","format-standard","has-post-thumbnail","category-kod-java","category-poradnik"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"https:\/\/www.juniorjavadeveloper.pl\/wp-content\/uploads\/2023\/01\/Java-wyrazenia-Lambda-dla-kazdego_kod-Java-krok-po-kroku_Mentor-Junior-Java-Developer.jpg","jetpack_sharing_enabled":true,"jetpack_likes_enabled":true,"_links":{"self":[{"href":"https:\/\/www.juniorjavadeveloper.pl\/en\/wp-json\/wp\/v2\/posts\/1810","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.juniorjavadeveloper.pl\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.juniorjavadeveloper.pl\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.juniorjavadeveloper.pl\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.juniorjavadeveloper.pl\/en\/wp-json\/wp\/v2\/comments?post=1810"}],"version-history":[{"count":130,"href":"https:\/\/www.juniorjavadeveloper.pl\/en\/wp-json\/wp\/v2\/posts\/1810\/revisions"}],"predecessor-version":[{"id":4643,"href":"https:\/\/www.juniorjavadeveloper.pl\/en\/wp-json\/wp\/v2\/posts\/1810\/revisions\/4643"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.juniorjavadeveloper.pl\/en\/wp-json\/wp\/v2\/media\/2431"}],"wp:attachment":[{"href":"https:\/\/www.juniorjavadeveloper.pl\/en\/wp-json\/wp\/v2\/media?parent=1810"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.juniorjavadeveloper.pl\/en\/wp-json\/wp\/v2\/categories?post=1810"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.juniorjavadeveloper.pl\/en\/wp-json\/wp\/v2\/tags?post=1810"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}