{"id":206,"date":"2020-03-02T16:00:00","date_gmt":"2020-03-02T15:00:00","guid":{"rendered":"https:\/\/www.codemotion.com\/magazine\/lazy-java\/"},"modified":"2021-12-23T14:09:31","modified_gmt":"2021-12-23T13:09:31","slug":"lazy-java","status":"publish","type":"post","link":"https:\/\/www.codemotion.com\/magazine\/backend\/lazy-java\/","title":{"rendered":"Lazy Java"},"content":{"rendered":"\t\t\t\t<div class=\"wp-block-uagb-table-of-contents uagb-toc__align-left uagb-toc__columns-1  uagb-block-efcacf29      \"\n\t\t\t\t\tdata-scroll= \"1\"\n\t\t\t\t\tdata-offset= \"30\"\n\t\t\t\t\tstyle=\"\"\n\t\t\t\t>\n\t\t\t\t<div class=\"uagb-toc__wrap\">\n\t\t\t\t\t\t<div class=\"uagb-toc__title\">\n\t\t\t\t\t\t\tTable Of Contents\t\t\t\t\t\t<\/div>\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<div class=\"uagb-toc__list-wrap \">\n\t\t\t\t\t\t<ol class=\"uagb-toc__list\"><li class=\"uagb-toc__list\"><a href=\"#introduction\" class=\"uagb-toc-link__trigger\">Introduction<\/a><li class=\"uagb-toc__list\"><a href=\"#functional-programming\" class=\"uagb-toc-link__trigger\">Functional Programming<\/a><li class=\"uagb-toc__list\"><a href=\"#lazy-evaluation-and-java-8-streams\" class=\"uagb-toc-link__trigger\">Lazy Evaluation and Java 8 Streams<\/a><\/ol>\t\t\t\t\t<\/div>\n\t\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\n\n\n<h2 class=\"wp-block-heading\">Introduction<\/h2>\n\n\n\n<p><strong>Laziness<\/strong> is one of the three great virtues of any programmer according to Larry Wall, author of the book <em>Programming Perl<\/em>. Consequently, it is no surprise that <a rel=\"noopener noreferrer\" href=\"https:\/\/milan2018.codemotionworld.com\/speaker\/485\/\" target=\"_blank\">Mario Fusco<\/a>, senior software engineer at Red Hat, highlighted the power of <strong>lazy evaluation in Java<\/strong>, which represents an interesting approach for code optimisation. The two ideas are in fact related: lazy programmers tend to delay things until they are really needed; and lazy evaluation consists of delaying the evaluation of an expression until its value is needed.<\/p>\n\n\n\n<p>Mario Fusco was invited to deliver a speech at <a rel=\"noopener noreferrer\" href=\"https:\/\/milan2018.codemotionworld.com\/conference\/\" target=\"_blank\">Codemotion Milan 2018<\/a>, and he focused on how to apply lazy evaluation in Java. In fact, Java methods are <strong>strict<\/strong>, since they evaluate their arguments as soon as they are passed to them. However, even if Java is mainly a strict language, there are some exceptions. For instance, boolean operators <code>&amp;&amp;<\/code> and <code>||<\/code> are lazy, since the second operand is not evaluated, the first is deemed sufficient for computing the resulting boolean value. The same considerations apply to <code>if-else<\/code>, ternary operator <code>?<\/code> :, <code>for<\/code>\/<code>while<\/code> loops, and the Java 8 streams.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Functional Programming<\/h2>\n\n\n\n<p>In general, we can exploit the support to <strong><a href=\"https:\/\/www.codemotion.com\/magazine\/dev-hub\/backend-dev\/lambda-calculus-functional-programming-with-no-tears\/\" class=\"ek-link\">functional programming<\/a><\/strong> in order to turn Java into a lazy language. To explain how, Mario Fusco started from a simple example, which re-implements the ternary operator:<\/p>\n\n\n\n<div style=\"background: #ffffff; overflow: auto; width: auto; padding: 15px;\">\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #333333;\">&lt;<\/span>T<span style=\"color: #333333;\">&gt;<\/span> T ternary<span style=\"color: #333333;\">(<\/span><span style=\"color: #333399; font-weight: bold;\">boolean<\/span> pred<span style=\"color: #333333;\">,<\/span> T first<span style=\"color: #333333;\">,<\/span> T second<span style=\"color: #333333;\">)<\/span> <span style=\"color: #333333;\">{<\/span>\n    <span style=\"color: #008800; font-weight: bold;\">if<\/span> <span style=\"color: #333333;\">(<\/span>pred<span style=\"color: #333333;\">)<\/span> <span style=\"color: #333333;\">{<\/span>\n        <span style=\"color: #008800; font-weight: bold;\">return<\/span> first<span style=\"color: #333333;\">;<\/span>\n    <span style=\"color: #333333;\">}<\/span> <span style=\"color: #008800; font-weight: bold;\">else<\/span> <span style=\"color: #333333;\">{<\/span>\n        <span style=\"color: #008800; font-weight: bold;\">return<\/span> second<span style=\"color: #333333;\">;<\/span>\n    <span style=\"color: #333333;\">}<\/span>\n<span style=\"color: #333333;\">}<\/span>\n\nString <span style=\"color: #0066bb; font-weight: bold;\">val1<\/span><span style=\"color: #333333;\">()<\/span> <span style=\"color: #333333;\">{<\/span>\n    <span style=\"color: #008800; font-weight: bold;\">return<\/span> <span style=\"background-color: #fff0f0;\">\"first\"<\/span><span style=\"color: #333333;\">;<\/span>\n<span style=\"color: #333333;\">}<\/span>\nString <span style=\"color: #0066bb; font-weight: bold;\">val2<\/span><span style=\"color: #333333;\">()<\/span> <span style=\"color: #333333;\">{<\/span>\n    <span style=\"color: #008800; font-weight: bold;\">return<\/span> <span style=\"background-color: #fff0f0;\">\"second\"<\/span><span style=\"color: #333333;\">;<\/span>\n<span style=\"color: #333333;\">}<\/span>\n\nString result1 <span style=\"color: #333333;\">=<\/span> bool <span style=\"color: #333333;\">?<\/span> val1<span style=\"color: #333333;\">()<\/span> <span style=\"color: #333333;\">:<\/span> val2<span style=\"color: #333333;\">();<\/span>\nString result2 <span style=\"color: #333333;\">=<\/span> ternary<span style=\"color: #333333;\">(<\/span>bool<span style=\"color: #333333;\">,<\/span> val1<span style=\"color: #333333;\">(),<\/span> val2<span style=\"color: #333333;\">());<\/span>\n<\/pre>\n<\/div>\n\n\n\n<p>The above code snippet is strict: indeed, in order to execute the <code>ternary<\/code> method, the arguments <code>val1()<\/code> and <code>val2()<\/code> need to be both evaluated. Consequently, we cannot obtain the desired behaviour with this strategy. However, <code>Supplier<\/code>s allows to turn the above code into lazy, as show by the following code snippet:<\/p>\n\n\n\n<div style=\"background: #ffffff; overflow: auto; width: auto; padding: 15px;\">\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #333333;\">&lt;<\/span>T<span style=\"color: #333333;\">&gt;<\/span> T ternary<span style=\"color: #333333;\">(<\/span><span style=\"color: #333399; font-weight: bold;\">boolean<\/span> pred<span style=\"color: #333333;\">,<\/span> Supplier<span style=\"color: #333333;\">&lt;<\/span>T<span style=\"color: #333333;\">&gt;<\/span> first<span style=\"color: #333333;\">,<\/span> Supplier<span style=\"color: #333333;\">&lt;<\/span>T<span style=\"color: #333333;\">&gt;<\/span> second<span style=\"color: #333333;\">)<\/span> <span style=\"color: #333333;\">{<\/span>\n    <span style=\"color: #008800; font-weight: bold;\">if<\/span> <span style=\"color: #333333;\">(<\/span>pred<span style=\"color: #333333;\">)<\/span> <span style=\"color: #333333;\">{<\/span>\n        <span style=\"color: #008800; font-weight: bold;\">return<\/span> first<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">get<\/span><span style=\"color: #333333;\">();<\/span>\n    <span style=\"color: #333333;\">}<\/span> <span style=\"color: #008800; font-weight: bold;\">else<\/span> <span style=\"color: #333333;\">{<\/span>\n        <span style=\"color: #008800; font-weight: bold;\">return<\/span> second<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">get<\/span><span style=\"color: #333333;\">();<\/span>\n    <span style=\"color: #333333;\">}<\/span>\n<span style=\"color: #333333;\">}<\/span>\n\nString <span style=\"color: #0066bb; font-weight: bold;\">val1<\/span><span style=\"color: #333333;\">()<\/span> <span style=\"color: #333333;\">{<\/span>\n    <span style=\"color: #008800; font-weight: bold;\">return<\/span> <span style=\"background-color: #fff0f0;\">\"first\"<\/span><span style=\"color: #333333;\">;<\/span>\n<span style=\"color: #333333;\">}<\/span>\nString <span style=\"color: #0066bb; font-weight: bold;\">val2<\/span><span style=\"color: #333333;\">()<\/span> <span style=\"color: #333333;\">{<\/span>\n    <span style=\"color: #008800; font-weight: bold;\">return<\/span> <span style=\"background-color: #fff0f0;\">\"second\"<\/span><span style=\"color: #333333;\">;<\/span>\n<span style=\"color: #333333;\">}<\/span>\n\nString result1 <span style=\"color: #333333;\">=<\/span> bool <span style=\"color: #333333;\">?<\/span> val1<span style=\"color: #333333;\">()<\/span> <span style=\"color: #333333;\">:<\/span> val2<span style=\"color: #333333;\">();<\/span>\nString result2 <span style=\"color: #333333;\">=<\/span> ternary<span style=\"color: #333333;\">(<\/span>bool<span style=\"color: #333333;\">,<\/span> <span style=\"color: #333333;\">()<\/span> <span style=\"color: #333333;\">-&gt;<\/span> val1<span style=\"color: #333333;\">(),<\/span> <span style=\"color: #333333;\">()<\/span> <span style=\"color: #333333;\">-&gt;<\/span> val2<span style=\"color: #333333;\">());<\/span>\n<\/pre>\n<\/div>\n\n\n\n<p>As you can see, optimising the code by exploiting lazy evaluation is quite easy. And accordingly to Mario Fusco, \u201claziness is probably the only form of performance optimisation which is (almost) never premature\u201d. Indeed, \u201cthere is nothing so useless as doing efficiently something that should not be done at all\u201d.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Lazy Evaluation and Java 8 Streams<\/h2>\n\n\n\n<p>As mentioned before, Java 8 Streams represent one of the construct that are lazy by definition. Consider the following example:<\/p>\n\n\n\n<div style=\"background: #ffffff; overflow: auto; width: auto; padding: 15px;\">\n<pre style=\"margin: 0; line-height: 125%;\">IntStream<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">iterate<\/span><span style=\"color: #333333;\">(<\/span> <span style=\"color: #0000dd; font-weight: bold;\">1<\/span><span style=\"color: #333333;\">,<\/span> i <span style=\"color: #333333;\">-&gt;<\/span> i<span style=\"color: #333333;\">+<\/span><span style=\"color: #0000dd; font-weight: bold;\">1<\/span> <span style=\"color: #333333;\">)<\/span>\n         <span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">map<\/span><span style=\"color: #333333;\">(<\/span> i <span style=\"color: #333333;\">-&gt;<\/span> i <span style=\"color: #333333;\">*<\/span> <span style=\"color: #0000dd; font-weight: bold;\">2<\/span> <span style=\"color: #333333;\">)<\/span>\n         <span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">filter<\/span><span style=\"color: #333333;\">(<\/span> i <span style=\"color: #333333;\">-&gt;<\/span> i <span style=\"color: #333333;\">&gt;<\/span> <span style=\"color: #0000dd; font-weight: bold;\">5<\/span> <span style=\"color: #333333;\">)<\/span>\n         <span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">findFirst<\/span><span style=\"color: #333333;\">();<\/span>\n<\/pre>\n<\/div>\n\n\n\n<p>Thanks to the lazy nature of streams, the <code>iterate<\/code> method allows to create streams that are potentially <strong>infinite<\/strong>. Indeed, this method does not create any data structure, but it demands the data generation to the terminal operation (in this case the <code>findFirst<\/code> method). Same considerations apply to the <code>map<\/code> and <code>filter<\/code> methods: intermediate operations are lazy, since they do not perform any action before the terminal operation. These considerations point to a key concept: <strong>streams are not data structures, but rather lazy specifications<\/strong> of how to manipulate data.<\/p>\n\n\n\n<p>Putting these ideas into practice should seem conceptually simpler now. However, this is not exactly the case. In fact, Java still has many limitations that, for instance, have been implemented differently in <strong>Scala<\/strong> or other languages more oriented towards <a href=\"https:\/\/www.codemotion.com\/magazine\/dev-hub\/backend-dev\/lsp-functional-programming\/\" target=\"_blank\" rel=\"noopener\">functional programming<\/a>. Mario Fusco <a href=\"https:\/\/www.slideshare.net\/mariofusco\/lazy-java\" target=\"_blank\" rel=\"noopener noreferrer\">shared his presentation on SlideShare<\/a>, where you can find many other interesting examples to understand the practical difficulties in using lazy evaluation in Java.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Introduction Laziness is one of the three great virtues of any programmer according to Larry Wall, author of the book Programming Perl. Consequently, it is no surprise that Mario Fusco, senior software engineer at Red Hat, highlighted the power of lazy evaluation in Java, which represents an interesting approach for code optimisation. The two ideas&#8230; <a class=\"more-link\" href=\"https:\/\/www.codemotion.com\/magazine\/backend\/lazy-java\/\">Read more<\/a><\/p>\n","protected":false},"author":3,"featured_media":207,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_editorskit_title_hidden":false,"_editorskit_reading_time":0,"_editorskit_is_block_options_detached":false,"_editorskit_block_options_position":"{}","_uag_custom_page_level_css":"","_genesis_hide_title":false,"_genesis_hide_breadcrumbs":false,"_genesis_hide_singular_image":false,"_genesis_hide_footer_widgets":false,"_genesis_custom_body_class":"","_genesis_custom_post_class":"","_genesis_layout":"","footnotes":""},"categories":[36],"tags":[22,3412,52],"collections":[],"class_list":{"0":"post-206","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-backend","8":"tag-codemotion-milan","9":"tag-functional-programming","10":"tag-java","11":"entry"},"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v26.9 (Yoast SEO v26.9) - https:\/\/yoast.com\/product\/yoast-seo-premium-wordpress\/ -->\n<title>Lazy Evaluation in Java: how to - Codemotion Magazine<\/title>\n<meta name=\"description\" content=\"At Codemotion Milan 2018, Mario Fusco delivered a speech to explain how to apply lazy evaluation in Java. In this article, we recap his discussion.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.codemotion.com\/magazine\/backend\/lazy-java\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Lazy Java\" \/>\n<meta property=\"og:description\" content=\"At Codemotion Milan 2018, Mario Fusco delivered a speech to explain how to apply lazy evaluation in Java. In this article, we recap his discussion.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.codemotion.com\/magazine\/backend\/lazy-java\/\" \/>\n<meta property=\"og:site_name\" content=\"Codemotion Magazine\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/Codemotion.Italy\/\" \/>\n<meta property=\"article:published_time\" content=\"2020-03-02T15:00:00+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2021-12-23T13:09:31+00:00\" \/>\n<meta name=\"author\" content=\"Vito Gentile\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:title\" content=\"Lazy Java\" \/>\n<meta name=\"twitter:image\" content=\"https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2019\/03\/47235483_2191619097527053_8680609529236291584_o.jpg\" \/>\n<meta name=\"twitter:creator\" content=\"@ViGentile\" \/>\n<meta name=\"twitter:site\" content=\"@CodemotionIT\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Vito Gentile\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/www.codemotion.com\/magazine\/backend\/lazy-java\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/www.codemotion.com\/magazine\/backend\/lazy-java\/\"},\"author\":{\"name\":\"Vito Gentile\",\"@id\":\"https:\/\/www.codemotion.com\/magazine\/#\/schema\/person\/f9c99d8dfb99900d709bb8199225d313\"},\"headline\":\"Lazy Java\",\"datePublished\":\"2020-03-02T15:00:00+00:00\",\"dateModified\":\"2021-12-23T13:09:31+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.codemotion.com\/magazine\/backend\/lazy-java\/\"},\"wordCount\":499,\"publisher\":{\"@id\":\"https:\/\/www.codemotion.com\/magazine\/#organization\"},\"image\":{\"@id\":\"https:\/\/www.codemotion.com\/magazine\/backend\/lazy-java\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2019\/03\/47235483_2191619097527053_8680609529236291584_o.jpg\",\"keywords\":[\"Codemotion Milan\",\"Functional Programming\",\"Java\"],\"articleSection\":[\"Backend\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.codemotion.com\/magazine\/backend\/lazy-java\/\",\"url\":\"https:\/\/www.codemotion.com\/magazine\/backend\/lazy-java\/\",\"name\":\"Lazy Evaluation in Java: how to - Codemotion Magazine\",\"isPartOf\":{\"@id\":\"https:\/\/www.codemotion.com\/magazine\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/www.codemotion.com\/magazine\/backend\/lazy-java\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/www.codemotion.com\/magazine\/backend\/lazy-java\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2019\/03\/47235483_2191619097527053_8680609529236291584_o.jpg\",\"datePublished\":\"2020-03-02T15:00:00+00:00\",\"dateModified\":\"2021-12-23T13:09:31+00:00\",\"description\":\"At Codemotion Milan 2018, Mario Fusco delivered a speech to explain how to apply lazy evaluation in Java. In this article, we recap his discussion.\",\"breadcrumb\":{\"@id\":\"https:\/\/www.codemotion.com\/magazine\/backend\/lazy-java\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.codemotion.com\/magazine\/backend\/lazy-java\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.codemotion.com\/magazine\/backend\/lazy-java\/#primaryimage\",\"url\":\"https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2019\/03\/47235483_2191619097527053_8680609529236291584_o.jpg\",\"contentUrl\":\"https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2019\/03\/47235483_2191619097527053_8680609529236291584_o.jpg\",\"width\":1013,\"height\":675},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.codemotion.com\/magazine\/backend\/lazy-java\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.codemotion.com\/magazine\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Backend\",\"item\":\"https:\/\/www.codemotion.com\/magazine\/backend\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Lazy Java\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.codemotion.com\/magazine\/#website\",\"url\":\"https:\/\/www.codemotion.com\/magazine\/\",\"name\":\"Codemotion Magazine\",\"description\":\"We code the future. Together\",\"publisher\":{\"@id\":\"https:\/\/www.codemotion.com\/magazine\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.codemotion.com\/magazine\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/www.codemotion.com\/magazine\/#organization\",\"name\":\"Codemotion\",\"url\":\"https:\/\/www.codemotion.com\/magazine\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.codemotion.com\/magazine\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2019\/11\/codemotionlogo.png\",\"contentUrl\":\"https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2019\/11\/codemotionlogo.png\",\"width\":225,\"height\":225,\"caption\":\"Codemotion\"},\"image\":{\"@id\":\"https:\/\/www.codemotion.com\/magazine\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/Codemotion.Italy\/\",\"https:\/\/x.com\/CodemotionIT\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.codemotion.com\/magazine\/#\/schema\/person\/f9c99d8dfb99900d709bb8199225d313\",\"name\":\"Vito Gentile\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.codemotion.com\/magazine\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/ebcb5b885a4f1669578d08a55deb81064893c07c1b35b7b36eb059cce730ae90?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/ebcb5b885a4f1669578d08a55deb81064893c07c1b35b7b36eb059cce730ae90?s=96&d=mm&r=g\",\"caption\":\"Vito Gentile\"},\"description\":\"I\u2019m a data scientist, tech writer, software developer with experience in mobile, web (full-stack) and Python programming, and former researcher with interests in human-computer interaction. I thus have a multi-faceted experience in the area of software development, and that\u2019s why I love my job(s)!\",\"sameAs\":[\"https:\/\/vitogentile.it\",\"https:\/\/www.linkedin.com\/in\/vitogentile\/en\",\"https:\/\/x.com\/ViGentile\"],\"url\":\"https:\/\/www.codemotion.com\/magazine\/author\/vito-gentile\/\"}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"Lazy Evaluation in Java: how to - Codemotion Magazine","description":"At Codemotion Milan 2018, Mario Fusco delivered a speech to explain how to apply lazy evaluation in Java. In this article, we recap his discussion.","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:\/\/www.codemotion.com\/magazine\/backend\/lazy-java\/","og_locale":"en_US","og_type":"article","og_title":"Lazy Java","og_description":"At Codemotion Milan 2018, Mario Fusco delivered a speech to explain how to apply lazy evaluation in Java. In this article, we recap his discussion.","og_url":"https:\/\/www.codemotion.com\/magazine\/backend\/lazy-java\/","og_site_name":"Codemotion Magazine","article_publisher":"https:\/\/www.facebook.com\/Codemotion.Italy\/","article_published_time":"2020-03-02T15:00:00+00:00","article_modified_time":"2021-12-23T13:09:31+00:00","author":"Vito Gentile","twitter_card":"summary_large_image","twitter_title":"Lazy Java","twitter_image":"https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2019\/03\/47235483_2191619097527053_8680609529236291584_o.jpg","twitter_creator":"@ViGentile","twitter_site":"@CodemotionIT","twitter_misc":{"Written by":"Vito Gentile","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.codemotion.com\/magazine\/backend\/lazy-java\/#article","isPartOf":{"@id":"https:\/\/www.codemotion.com\/magazine\/backend\/lazy-java\/"},"author":{"name":"Vito Gentile","@id":"https:\/\/www.codemotion.com\/magazine\/#\/schema\/person\/f9c99d8dfb99900d709bb8199225d313"},"headline":"Lazy Java","datePublished":"2020-03-02T15:00:00+00:00","dateModified":"2021-12-23T13:09:31+00:00","mainEntityOfPage":{"@id":"https:\/\/www.codemotion.com\/magazine\/backend\/lazy-java\/"},"wordCount":499,"publisher":{"@id":"https:\/\/www.codemotion.com\/magazine\/#organization"},"image":{"@id":"https:\/\/www.codemotion.com\/magazine\/backend\/lazy-java\/#primaryimage"},"thumbnailUrl":"https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2019\/03\/47235483_2191619097527053_8680609529236291584_o.jpg","keywords":["Codemotion Milan","Functional Programming","Java"],"articleSection":["Backend"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/www.codemotion.com\/magazine\/backend\/lazy-java\/","url":"https:\/\/www.codemotion.com\/magazine\/backend\/lazy-java\/","name":"Lazy Evaluation in Java: how to - Codemotion Magazine","isPartOf":{"@id":"https:\/\/www.codemotion.com\/magazine\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.codemotion.com\/magazine\/backend\/lazy-java\/#primaryimage"},"image":{"@id":"https:\/\/www.codemotion.com\/magazine\/backend\/lazy-java\/#primaryimage"},"thumbnailUrl":"https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2019\/03\/47235483_2191619097527053_8680609529236291584_o.jpg","datePublished":"2020-03-02T15:00:00+00:00","dateModified":"2021-12-23T13:09:31+00:00","description":"At Codemotion Milan 2018, Mario Fusco delivered a speech to explain how to apply lazy evaluation in Java. In this article, we recap his discussion.","breadcrumb":{"@id":"https:\/\/www.codemotion.com\/magazine\/backend\/lazy-java\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.codemotion.com\/magazine\/backend\/lazy-java\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.codemotion.com\/magazine\/backend\/lazy-java\/#primaryimage","url":"https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2019\/03\/47235483_2191619097527053_8680609529236291584_o.jpg","contentUrl":"https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2019\/03\/47235483_2191619097527053_8680609529236291584_o.jpg","width":1013,"height":675},{"@type":"BreadcrumbList","@id":"https:\/\/www.codemotion.com\/magazine\/backend\/lazy-java\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.codemotion.com\/magazine\/"},{"@type":"ListItem","position":2,"name":"Backend","item":"https:\/\/www.codemotion.com\/magazine\/backend\/"},{"@type":"ListItem","position":3,"name":"Lazy Java"}]},{"@type":"WebSite","@id":"https:\/\/www.codemotion.com\/magazine\/#website","url":"https:\/\/www.codemotion.com\/magazine\/","name":"Codemotion Magazine","description":"We code the future. Together","publisher":{"@id":"https:\/\/www.codemotion.com\/magazine\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.codemotion.com\/magazine\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.codemotion.com\/magazine\/#organization","name":"Codemotion","url":"https:\/\/www.codemotion.com\/magazine\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.codemotion.com\/magazine\/#\/schema\/logo\/image\/","url":"https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2019\/11\/codemotionlogo.png","contentUrl":"https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2019\/11\/codemotionlogo.png","width":225,"height":225,"caption":"Codemotion"},"image":{"@id":"https:\/\/www.codemotion.com\/magazine\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/Codemotion.Italy\/","https:\/\/x.com\/CodemotionIT"]},{"@type":"Person","@id":"https:\/\/www.codemotion.com\/magazine\/#\/schema\/person\/f9c99d8dfb99900d709bb8199225d313","name":"Vito Gentile","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.codemotion.com\/magazine\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/ebcb5b885a4f1669578d08a55deb81064893c07c1b35b7b36eb059cce730ae90?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/ebcb5b885a4f1669578d08a55deb81064893c07c1b35b7b36eb059cce730ae90?s=96&d=mm&r=g","caption":"Vito Gentile"},"description":"I\u2019m a data scientist, tech writer, software developer with experience in mobile, web (full-stack) and Python programming, and former researcher with interests in human-computer interaction. I thus have a multi-faceted experience in the area of software development, and that\u2019s why I love my job(s)!","sameAs":["https:\/\/vitogentile.it","https:\/\/www.linkedin.com\/in\/vitogentile\/en","https:\/\/x.com\/ViGentile"],"url":"https:\/\/www.codemotion.com\/magazine\/author\/vito-gentile\/"}]}},"featured_image_src":"https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2019\/03\/47235483_2191619097527053_8680609529236291584_o-600x400.jpg","featured_image_src_square":"https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2019\/03\/47235483_2191619097527053_8680609529236291584_o-600x600.jpg","author_info":{"display_name":"Vito Gentile","author_link":"https:\/\/www.codemotion.com\/magazine\/author\/vito-gentile\/"},"uagb_featured_image_src":{"full":["https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2019\/03\/47235483_2191619097527053_8680609529236291584_o.jpg",1013,675,false],"thumbnail":["https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2019\/03\/47235483_2191619097527053_8680609529236291584_o-150x150.jpg",150,150,true],"medium":["https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2019\/03\/47235483_2191619097527053_8680609529236291584_o-300x200.jpg",300,200,true],"medium_large":["https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2019\/03\/47235483_2191619097527053_8680609529236291584_o-768x512.jpg",768,512,true],"large":["https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2019\/03\/47235483_2191619097527053_8680609529236291584_o.jpg",1013,675,false],"1536x1536":["https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2019\/03\/47235483_2191619097527053_8680609529236291584_o.jpg",1013,675,false],"2048x2048":["https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2019\/03\/47235483_2191619097527053_8680609529236291584_o.jpg",1013,675,false],"small-home-featured":["https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2019\/03\/47235483_2191619097527053_8680609529236291584_o.jpg",100,67,false],"sidebar-featured":["https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2019\/03\/47235483_2191619097527053_8680609529236291584_o-180x128.jpg",180,128,true],"genesis-singular-images":["https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2019\/03\/47235483_2191619097527053_8680609529236291584_o-896x504.jpg",896,504,true],"archive-featured":["https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2019\/03\/47235483_2191619097527053_8680609529236291584_o-400x225.jpg",400,225,true],"gb-block-post-grid-landscape":["https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2019\/03\/47235483_2191619097527053_8680609529236291584_o-600x400.jpg",600,400,true],"gb-block-post-grid-square":["https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2019\/03\/47235483_2191619097527053_8680609529236291584_o-600x600.jpg",600,600,true]},"uagb_author_info":{"display_name":"Vito Gentile","author_link":"https:\/\/www.codemotion.com\/magazine\/author\/vito-gentile\/"},"uagb_comment_info":0,"uagb_excerpt":"Introduction Laziness is one of the three great virtues of any programmer according to Larry Wall, author of the book Programming Perl. Consequently, it is no surprise that Mario Fusco, senior software engineer at Red Hat, highlighted the power of lazy evaluation in Java, which represents an interesting approach for code optimisation. The two ideas&#8230;&hellip;","lang":"en","_links":{"self":[{"href":"https:\/\/www.codemotion.com\/magazine\/wp-json\/wp\/v2\/posts\/206","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.codemotion.com\/magazine\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.codemotion.com\/magazine\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.codemotion.com\/magazine\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/www.codemotion.com\/magazine\/wp-json\/wp\/v2\/comments?post=206"}],"version-history":[{"count":8,"href":"https:\/\/www.codemotion.com\/magazine\/wp-json\/wp\/v2\/posts\/206\/revisions"}],"predecessor-version":[{"id":14200,"href":"https:\/\/www.codemotion.com\/magazine\/wp-json\/wp\/v2\/posts\/206\/revisions\/14200"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.codemotion.com\/magazine\/wp-json\/wp\/v2\/media\/207"}],"wp:attachment":[{"href":"https:\/\/www.codemotion.com\/magazine\/wp-json\/wp\/v2\/media?parent=206"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.codemotion.com\/magazine\/wp-json\/wp\/v2\/categories?post=206"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.codemotion.com\/magazine\/wp-json\/wp\/v2\/tags?post=206"},{"taxonomy":"collections","embeddable":true,"href":"https:\/\/www.codemotion.com\/magazine\/wp-json\/wp\/v2\/collections?post=206"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}