{"id":1510,"date":"2021-04-26T09:07:19","date_gmt":"2021-04-26T09:07:19","guid":{"rendered":"https:\/\/blog.samarthya.me\/wps\/?p=1510"},"modified":"2021-04-26T09:07:21","modified_gmt":"2021-04-26T09:07:21","slug":"java-stream-examples","status":"publish","type":"post","link":"https:\/\/blog.samarthya.me\/wps\/2021\/04\/26\/java-stream-examples\/","title":{"rendered":"Java Stream: Examples"},"content":{"rendered":"<p>Streams are convenient way to reduce the line of codes, but an understanding them is equally challenging. I write this blog, after realising a desperate attempt at solving a trivial problem just took overwhelming time. (Could be that I am getting old, but never late to learn things again)<\/p>\n<p>\u00a0<\/p>\n\n\n<pre class=\"wp-block-code\"><code>public class PlaySteams {\n    public static void main(String &#91;]args) {\n        int&#91;] a = new int&#91;] {1,1,1,2,2,2,3,3,3,4,4,4};\n        \/\/Stream Sum of the array\n        long sum = IntStream.range(0, a.length).count();\n\n        System.out.printf(\"Sum: %d\\n\", sum);\n        \/\/ Maximum\n        System.out.printf(\"Max: %d\\n\", IntStream.range(0, a.length).map(i -> Integer.valueOf(a&#91;i])).distinct().max().getAsInt());\n        \/\/ Minimum\n        System.out.printf(\"Min: %d\\n\", IntStream.range(0, a.length).map(i -> Integer.valueOf(a&#91;i])).distinct().min().getAsInt());\n\n        IntStream.range(0, a.length).map(i -> Integer.valueOf(a&#91;i])).distinct().forEach(x -> System.out.printf(\"%d \", x));\n        System.out.printf(\"Average (Distinct): %f\\n\", IntStream.range(0, a.length).map(i -> Integer.valueOf(a&#91;i])).distinct().average().getAsDouble());\n\n        System.out.printf(\" Number of elements : %d\\n\", IntStream.of(a).asLongStream().count());\n\n        IntStream.range(0, a.length).map(i -> Integer.valueOf(a&#91;i])).forEach(x -> System.out.printf(\"%d \", x));\n        System.out.printf(\"Average (All): %f\\n\", IntStream.range(0, a.length).map(i -> Integer.valueOf(a&#91;i])).average().getAsDouble());\n\n        List&lt;Integer> listOfIntegers = IntStream.of(a).boxed().collect(Collectors.toList());\n        System.out.println(listOfIntegers.toString());\n\n        System.out.println(\"Length: \");\n        System.out.println(listOfIntegers.size());\n\n        \/\/ Creating a map\n        \/\/ 1 -> 3\n        \/\/ 2 -> 3 ...\n        System.out.println(IntStream.range(0, a.length).map(i -> a&#91;i]).boxed().collect(Collectors.groupingBy(Function.identity(), Collectors.counting())).toString());\n\n        \/\/ Using list\n        Map&lt;Integer, Long> valueMap = listOfIntegers.stream().collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));\n        System.out.println(valueMap.toString());\n\n        \/\/ Map of indexes\n        \/\/ Indexes and value of the index\n        System.out.println(IntStream.range(0, a.length).boxed().collect(Collectors.groupingBy( i -> a&#91;Integer.valueOf(i)], Collectors.toList())));\n\n\n    }\n}<\/code><\/pre>\n\n\n\n<p>The example set remains the same for all the invocation.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>int&#91;] a = new int&#91;] {1,1,1,2,2,2,3,3,3,4,4,4};<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Example &#8211; 1:<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>long sum = IntStream.range(0, a.length).count();\nSystem.out.printf(\"Sum: %d\\n\", sum);<\/code><\/pre>\n\n\n\n<p>It simply runs over the length of the array and returns the total number of elements<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Sum: 12<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Example &#8211; 2:<\/h2>\n\n\n\n<p>Return the maximum<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>System.out.printf(\"Max: %d\\n\", IntStream.range(0, a.length).map(i -> Integer.valueOf(a&#91;i])).distinct().max().getAsInt());<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>Max: 4<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Example &#8211; 3:<\/h2>\n\n\n\n<p>Returns the minimum<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>System.out.printf(\"Min: %d\\n\", IntStream.range(0, a.length).map(i -> Integer.valueOf(a&#91;i])).distinct().min().getAsInt());<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>Min: 1<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Example &#8211; 4:<\/h2>\n\n\n\n<p>Run through the list and print distinct items<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>IntStream.range(0, a.length).map(i -> Integer.valueOf(a&#91;i])).distinct().forEach(\nx -> \nSystem.out.printf(\"%d \", x));<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>1 2 3 4 <\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Example &#8211; 5:<\/h2>\n\n\n\n<p>Average of the distinct items<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>System.out.printf(\"Average (Distinct): %f\\n\", IntStream.range(0, a.length).map(i -> Integer.valueOf(a&#91;i])).distinct().average().getAsDouble());<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>Average (Distinct): 2.500000<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Example &#8211; 6:<\/h2>\n\n\n\n<p>Different way of getting the count of elements.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>System.out.printf(\" Number of elements : %d\\n\", IntStream.of(a).asLongStream().count());<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>Number of elements : 12<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Example &#8211; 7:<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>IntStream.range(0, a.length).map(i -> Integer.valueOf(a&#91;i])).forEach(x -> System.out.printf(\"%d \", x));<\/code><\/pre>\n\n\n\n<p>Get the list of elements<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>1 1 1 2 2 2 3 3 3 4 4 4 <\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Example &#8211; 8:<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>System.out.printf(\"Average (All): %f\\n\", IntStream.range(0, a.length).map(i -> Integer.valueOf(a&#91;i])).average().getAsDouble());<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>Average (All): 2.500000<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Example &#8211; 9:<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>List&lt;Integer> listOfIntegers = IntStream.of(a).boxed().collect(Collectors.toList());\nSystem.out.println(listOfIntegers.toString());<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>&#91;1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4]<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Example &#8211; 10:<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>System.out.println(IntStream.range(0, a.length).map(i -> a&#91;i]).boxed().collect(Collectors.groupingBy(Function.identity(), Collectors.counting())).toString());<\/code><\/pre>\n\n\n\n<p>Creates a map of the value and the number of times it repeats<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>1=3, 2=3, 3=3, 4=3}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Example &#8211; 11:<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>Map&lt;Integer, Long> valueMap = listOfIntegers.stream().collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));\n        System.out.println(valueMap.toString());<\/code><\/pre>\n\n\n\n<p>Using list that we had in <code>Example - 9<\/code> to get the map.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>{1=3, 2=3, 3=3, 4=3}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Example &#8211; 12:<\/h2>\n\n\n\n<p>Returns the list of index for each value<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>System.out.println(IntStream.range(0, a.length).boxed().collect(Collectors.groupingBy( i -> a&#91;Integer.valueOf(i)], Collectors.toList())));<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>{1=&#91;0, 1, 2], 2=&#91;3, 4, 5], 3=&#91;6, 7, 8], 4=&#91;9, 10, 11]}<\/code><\/pre>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Streams are convenient way to reduce the line of codes, but an understanding them is equally challenging. I write this blog, after realising a desperate attempt at solving a trivial problem just took overwhelming time. (Could be that I am getting old, but never late to learn things again) \u00a0 The example set remains the [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":1396,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"image","meta":{"_exactmetrics_skip_tracking":false,"_exactmetrics_sitenote_active":false,"_exactmetrics_sitenote_note":"","_exactmetrics_sitenote_category":0,"footnotes":""},"categories":[120,34],"tags":[122],"class_list":["post-1510","post","type-post","status-publish","format-image","has-post-thumbnail","hentry","category-java","category-technical","tag-streams","post_format-post-format-image"],"_links":{"self":[{"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/posts\/1510","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/comments?post=1510"}],"version-history":[{"count":0,"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/posts\/1510\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/media\/1396"}],"wp:attachment":[{"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/media?parent=1510"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/categories?post=1510"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/tags?post=1510"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}