{"id":6847,"date":"2019-04-06T10:31:19","date_gmt":"2019-04-06T10:31:19","guid":{"rendered":"http:\/\/putridparrot.com\/blog\/?p=6847"},"modified":"2019-04-06T10:31:19","modified_gmt":"2019-04-06T10:31:19","slug":"javas-linq-equivalent-streams","status":"publish","type":"post","link":"https:\/\/putridparrot.com\/blog\/javas-linq-equivalent-streams\/","title":{"rendered":"Java&#8217;s Linq equivalent, Streams"},"content":{"rendered":"<p>I was working on a previous post on Java and GraphQL and wanted to use a Linq query. Java doesn&#8217;t have Linq but it does have something equivalent (in Java 8) called Stream. For the examples within this post we&#8217;ll create the following simple method for generating our test data<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nprivate static List&lt;String&gt; getData() {\r\n   return Arrays.asList(\r\n      &quot;2&quot;, &quot;2&quot;, &quot;2&quot;, &quot;4&quot;, &quot;4&quot;, \r\n      &quot;6&quot;, &quot;7&quot;, &quot;7&quot;, &quot;9&quot;, &quot;10&quot;);\r\n}\r\n<\/pre>\n<p>Let&#8217;s look at some of the standard sort of functionality&#8230;<\/p>\n<p><strong>Distinct<\/strong><\/p>\n<p>We can get a distinct stream from our data using<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nStream&lt;String&gt; distinct = getData()\r\n   .stream()\r\n   .distinct();\r\n<\/pre>\n<p><strong>Selection\/Filtering<\/strong><\/p>\n<p>If we want to filter our data to select only strings longer than one character, for example, then we can use the following<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nStream&lt;String&gt; where = getData()\r\n   .stream()\r\n   .filter(s -&gt; s.length() &gt; 1);\r\n<\/pre>\n<p><strong>Matching<\/strong><\/p>\n<p>There are several methods which return a Boolean, for example if all items in the Stream are of a certain value, or any of the values no matches, for example<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nboolean allMatch = getData()\r\n   .stream()\r\n   .allMatch(s -&gt; s.contains(&quot;4&quot;));\r\n\r\nboolean anyMatch = getData()\r\n   .stream()\r\n   .anyMatch(s -&gt; s.contains(&quot;4&quot;));\r\n\r\nboolean noneMatch = getData()\r\n   .stream()\r\n   .noneMatch(s -&gt; s.contains(&quot;4&quot;));\r\n<\/pre>\n<p><strong>Map<\/strong><\/p>\n<p>In scenarios where we wish to change the data returned from a stream (i.e. like Select in Linq), for example we&#8217;ll simply loop through each item and return the length of each string as a Stream, we can use <\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nStream&lt;Integer&gt; select = getData()\r\n   .stream()\r\n   .map(String::length);\r\n<\/pre>\n<p><strong>Reduce<\/strong><\/p>\n<p>If we want to take a stream and reduce it to a single value, for example taking our data and concatenating into a single String, we could use the following <\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nString reduce = getData()\r\n   .stream()\r\n   .reduce(&quot;&quot;, (a, b) -&gt; a + b);\r\n<\/pre>\n<p>The initial value &#8220;&#8221; in this case is a starting value for the reduced value, in this case we&#8217;re not wanting to prepend anything to our string. The result of the reduce is a String with the value 22244677910.<\/p>\n<p><strong>Creating an empty Stream<\/strong><\/p>\n<p>We can create an empty Stream (useful in those situations where we do not wish to return a null) using<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nStream&lt;String&gt; empty = Stream.empty();\r\n<\/pre>\n<p><strong>Iteration<\/strong><\/p>\n<p>We can iterate over the Stream using the forEach method, for example<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\ngetData()\r\n   .stream()\r\n   .forEach(s -&gt; System.out.println(s));\r\n<\/pre>\n<p><strong>Parallel Streams<\/strong><\/p>\n<p>We can also parallelize (is that a word) our Stream using the <em>parallelStream<\/em>, for example<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nStream&lt;Integer&gt; parallel = getData()\r\n   .parallelStream()\r\n   .map(String::length);\r\n<\/pre>\n<p>As you&#8217;d expect the order or processing of the <em>map<\/em> in this instance is indeterminate.<\/p>\n<p>Obviously there&#8217;s more to Stream than I&#8217;ve listed here, but you get the idea.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I was working on a previous post on Java and GraphQL and wanted to use a Linq query. Java doesn&#8217;t have Linq but it does have something equivalent (in Java 8) called Stream. For the examples within this post we&#8217;ll create the following simple method for generating our test data private static List&lt;String&gt; getData() { [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[161],"tags":[],"class_list":["post-6847","post","type-post","status-publish","format-standard","hentry","category-java"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/6847","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/comments?post=6847"}],"version-history":[{"count":3,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/6847\/revisions"}],"predecessor-version":[{"id":6884,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/6847\/revisions\/6884"}],"wp:attachment":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/media?parent=6847"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/categories?post=6847"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/tags?post=6847"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}