{"id":518,"date":"2013-04-14T01:53:25","date_gmt":"2013-04-14T01:53:25","guid":{"rendered":"http:\/\/choudhury.com\/blog\/?p=518"},"modified":"2013-04-14T01:53:25","modified_gmt":"2013-04-14T01:53:25","slug":"java-8-streams-intro","status":"publish","type":"post","link":"https:\/\/choudhury.com\/blog\/2013\/04\/14\/java-8-streams-intro\/","title":{"rendered":"Java 8 Streams Intro"},"content":{"rendered":"<p>One of the cool new features in upcoming <a title=\"Java 8\" href=\"http:\/\/jdk8.java.net\/lambda\/\">Java 8<\/a> release is the update to the the Collections API with the new Streams API.<\/p>\n<p>Say if I have a Project class with an isCompleted method. If want a list of all completed projects. Now I want to retrieve a list of projects that have been completed.<\/p>\n<p><strong>Using Java 6 I could do this&#8230;.<\/strong><\/p>\n<div class=\"codecolorer-container java default\" style=\"overflow:auto;white-space:nowrap;\"><div class=\"java codecolorer\">List<span class=\"sy0\">&lt;<\/span>Project<span class=\"sy0\">&gt;<\/span> completedProjects<span class=\"sy0\">=<\/span><span class=\"kw1\">new<\/span> ArrayList<span class=\"sy0\">&lt;<\/span>Project<span class=\"sy0\">&gt;<\/span><span class=\"br0\">&#40;<\/span><span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span><br \/>\n<span class=\"kw1\">for<\/span> <span class=\"br0\">&#40;<\/span>Project project <span class=\"sy0\">:<\/span> allProjects<span class=\"br0\">&#41;<\/span> <span class=\"br0\">&#123;<\/span><br \/>\n&nbsp; &nbsp; <span class=\"kw1\">if<\/span> <span class=\"br0\">&#40;<\/span>project.<span class=\"me1\">isCompleted<\/span><span class=\"br0\">&#40;<\/span><span class=\"br0\">&#41;<\/span><span class=\"br0\">&#41;<\/span> <span class=\"br0\">&#123;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; completedProjects.<span class=\"me1\">add<\/span><span class=\"br0\">&#40;<\/span>project<span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span><br \/>\n&nbsp; &nbsp; <span class=\"br0\">&#125;<\/span><br \/>\n<span class=\"br0\">&#125;<\/span><\/div><\/div>\n<p>The above is correct and would work, few criticism of this way, many people (especially with functional language background) would argue its very verbose for what it&#8217;s doing, the problem that I&#8217;m trying to solve is just to get the released projects &#8211; ( do I need to specify it needs to be done in a loop?).<\/p>\n<p>So a few issues with the above<\/p>\n<ol>\n<li>Have to do the filtering using a loop, is something I shouldn&#8217;t have to decide, instead I should delegate it to library to decide the best implementation<\/li>\n<li>The looping used above is sequential, so doesn&#8217;t take advantage of multi-cores in todays machines<\/li>\n<li>Being pedantic, I had to mention the the type &#8220;Project&#8221; 3 times in this simple code (ok, with Java 7 Diamonds you could reduce it to 2).<\/li>\n<\/ol>\n<p><strong>The Java 8 Way of doing this would be..<\/strong><\/p>\n<div class=\"codecolorer-container java default\" style=\"overflow:auto;white-space:nowrap;\"><div class=\"java codecolorer\">List<span class=\"sy0\">&lt;<\/span>Project<span class=\"sy0\">&gt;<\/span> completedProjects <span class=\"sy0\">=<\/span> allProjects.<span class=\"me1\">stream<\/span><span class=\"br0\">&#40;<\/span><span class=\"br0\">&#41;<\/span><br \/>\n&nbsp; &nbsp; .<span class=\"me1\">filter<\/span><span class=\"br0\">&#40;<\/span>project <span class=\"sy0\">-&gt;<\/span> project.<span class=\"me1\">isCompleted<\/span><span class=\"br0\">&#40;<\/span><span class=\"br0\">&#41;<\/span><span class=\"br0\">&#41;<\/span><br \/>\n&nbsp; &nbsp; .<span class=\"me1\">collect<\/span><span class=\"br0\">&#40;<\/span>Collectors.<span class=\"me1\">toList<\/span><span class=\"br0\">&#40;<\/span><span class=\"br0\">&#41;<\/span><span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span><\/div><\/div>\n<p>Now the stream library is free to decide if it&#8217;s going to use sequential loop or use some clever algorithm to best use the CPU on the machine. i also like the fact, I only have to mention the type &#8220;Project&#8221; once in all of this.<\/p>\n<p>What confused me at first, is why the .collect is required the end. The way I think about this, is that Stream API could be applying the filter in parallel, so you need a collector at the end to pickup all of those results from the filters and place them into the result list.<\/p>\n<p>The neat thing about the streams, is that you add other stuff onto the pipeline, say, I want to get the sorted list of completed projects&#8230;<\/p>\n<div class=\"codecolorer-container java default\" style=\"overflow:auto;white-space:nowrap;\"><div class=\"java codecolorer\">List<span class=\"sy0\">&lt;<\/span>Project<span class=\"sy0\">&gt;<\/span> completedProjects <span class=\"sy0\">=<\/span> allProjects.<span class=\"me1\">stream<\/span><span class=\"br0\">&#40;<\/span><span class=\"br0\">&#41;<\/span><br \/>\n&nbsp; &nbsp; .<span class=\"me1\">filter<\/span><span class=\"br0\">&#40;<\/span>project <span class=\"sy0\">-&gt;<\/span> project.<span class=\"me1\">isCompleted<\/span><span class=\"br0\">&#40;<\/span><span class=\"br0\">&#41;<\/span><span class=\"br0\">&#41;<\/span><br \/>\n&nbsp; &nbsp; .<span class=\"me1\">sorted<\/span><span class=\"br0\">&#40;<\/span><span class=\"br0\">&#41;<\/span><br \/>\n&nbsp; &nbsp; .<span class=\"me1\">collect<\/span><span class=\"br0\">&#40;<\/span>Collectors.<span class=\"me1\">toList<\/span><span class=\"br0\">&#40;<\/span><span class=\"br0\">&#41;<\/span><span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span><\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>One of the cool new features in upcoming Java 8 release is the update to the the Collections API with the new Streams API. Say if I have a Project class with an isCompleted method. If want a list of all completed projects. Now I want to retrieve a list of projects that have been &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/choudhury.com\/blog\/2013\/04\/14\/java-8-streams-intro\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Java 8 Streams Intro&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[3,17],"tags":[],"class_list":["post-518","post","type-post","status-publish","format-standard","hentry","category-java","category-lambda"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/choudhury.com\/blog\/wp-json\/wp\/v2\/posts\/518","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/choudhury.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/choudhury.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/choudhury.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/choudhury.com\/blog\/wp-json\/wp\/v2\/comments?post=518"}],"version-history":[{"count":34,"href":"https:\/\/choudhury.com\/blog\/wp-json\/wp\/v2\/posts\/518\/revisions"}],"predecessor-version":[{"id":552,"href":"https:\/\/choudhury.com\/blog\/wp-json\/wp\/v2\/posts\/518\/revisions\/552"}],"wp:attachment":[{"href":"https:\/\/choudhury.com\/blog\/wp-json\/wp\/v2\/media?parent=518"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/choudhury.com\/blog\/wp-json\/wp\/v2\/categories?post=518"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/choudhury.com\/blog\/wp-json\/wp\/v2\/tags?post=518"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}