{"id":4258,"date":"2021-05-13T12:25:28","date_gmt":"2021-05-13T12:25:28","guid":{"rendered":"https:\/\/codethataint.com\/blog\/?p=4258"},"modified":"2021-05-13T14:43:42","modified_gmt":"2021-05-13T14:43:42","slug":"stream-code-interview-questions","status":"publish","type":"post","link":"https:\/\/codethataint.com\/blog\/stream-code-interview-questions\/","title":{"rendered":"Stream Code Interview Questions"},"content":{"rendered":"<ol>\n<li><strong>What is the Difference between Map and Filter?<\/strong><br \/>\nBoth perform intermediate Operations and returns stream as output.By using map, you transform the object values.<br \/>\n<strong>Map <\/strong>returns a stream consisting of the results of applying the given function to the elements of this stream. In a simple sentence, the map returns the transformed object value.<br \/>\n<strong>Filter <\/strong>is used for filtering the data, it always returns the boolean value. If it returns true, the item is added to list else it is filtered out (ignored)\n<\/li>\n<li><strong>Find the Sum of Nos in List?<\/strong>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npublic class ReduceEgs {\r\n    public static void main(String&#x5B;] args) {\r\n        List&lt;Integer&gt; arrNumbers =  Arrays.asList(1,2,3,4,5,6,7,8,9);\r\n\r\n        Integer sum = arrNumbers.stream().reduce(0, Integer::sum);\r\n        Integer sum2 = arrNumbers.stream().reduce(0, (x,y)-&gt;x+y);\r\n        Integer sum3 = arrNumbers.stream().reduce(0, ReduceEgs::sumNos);\r\n    }\r\n\r\n    public static Integer sumNos(int x, int y){\r\n        System.out.println(&quot;a -&quot;+ x + &quot; b - &quot;+  y);\r\n        return x+y;\r\n    }\r\n}\r\n<\/pre>\n<\/li>\n<li><strong>Find the Min and Max in Nos List?<\/strong>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\n public static void main(String&#x5B;] args) {\r\n        List&lt;Integer&gt; arrNumbers =  Arrays.asList(1,2,3,4,5,-6,7,-8,9);\r\n\r\n        arrNumbers =  Arrays.asList(-5,-6,-8);\r\n        Integer Max = arrNumbers.stream().reduce(0, (x,y)-&gt;x&gt;y?x:y);\r\n        System.out.println(&quot;Output 1 =&quot;+ Max);\r\n\r\n        \/\/Its good to start with Identity as Negative Value in order to address negative Integer Comparison\r\n        \/\/If you start with 0 then 0 would be returned like above\r\n        Integer Max2 = arrNumbers.stream().reduce(Integer.MIN_VALUE, (x,y)-&gt;x&gt;y?x:y);\r\n        System.out.println(&quot;Output 2 =&quot;+ Max2);\r\n\r\n        arrNumbers =  Arrays.asList(5,6,8);\r\n\r\n        Integer Min = arrNumbers.stream().reduce(0, (x,y)-&gt;x&lt;y?x:y);\r\n        System.out.println(&quot;Output 3 =&quot;+ Min);\r\n\r\n        \/\/Its good to start with Identity as Max Positive Value in order to address Integer Comparison\r\n        Integer Min2 = arrNumbers.stream().reduce(Integer.MAX_VALUE, (x,y)-&gt;x&lt;y?x:y);\r\n        System.out.println(&quot;Output 4 =&quot;+ Min2);\r\n    }\r\n<\/pre>\n<p><strong>Output<\/strong><\/p>\n<pre>\r\nOutput 1 =0\r\nOutput 2 =-5\r\nOutput 3 =0\r\nOutput 4 =5\r\n<\/pre>\n<\/li>\n<li><strong>Find the Sum of Square of Nos in List?<\/strong>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npublic class ReduceEgs {\r\n    public static void main(String&#x5B;] args) {\r\n        List&lt;Integer&gt; arrNumbers =  Arrays.asList(1,2,3);\r\n\r\n        Integer squareSum = arrNumbers.stream().map(x-&gt;x*x).reduce(0, Integer::sum);\r\n        System.out.println(&quot;Output 1 =&quot;+ squareSum);\r\n    }\r\n}\r\n<\/pre>\n<p><strong>Output<\/strong><\/p>\n<pre>\r\nOutput 1 =14\r\n<\/pre>\n<\/li>\n<li><strong>Find the Sum of Odd Nos in List?<\/strong>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npublic class ReduceEgs {\r\n    public static void main(String&#x5B;] args) {\r\n        List&lt;Integer&gt; arrNumbers =  Arrays.asList(1,2,3);\r\n\r\n        Integer squareSum = arrNumbers.stream().filter(x-&gt; x%2==1).reduce(0, Integer::sum);\r\n        System.out.println(&quot;Output 1 =&quot;+ squareSum);\r\n    }\r\n}\r\n<\/pre>\n<p><strong>Output<\/strong><\/p>\n<pre>\r\nOutput 1 =4\r\n<\/pre>\n<\/li>\n<li><strong>Distinct elements from List?<\/strong>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npublic class ReduceEgs {\r\n    public static void main(String&#x5B;] args) {\r\n        List&lt;Integer&gt; arrNumbers =  Arrays.asList(1,2,3,1,4,2);\r\n\r\n        System.out.println(&quot;Output 1&quot;);\r\n        arrNumbers.stream().distinct().forEach(System.out::println);\r\n    }\r\n}\r\n\r\n<\/pre>\n<p><strong>Output<\/strong><\/p>\n<pre>\r\nOutput 1\r\n1\r\n2\r\n3\r\n4\r\n<\/pre>\n<\/li>\n<li><strong>Sort Elements in List?<\/strong>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\n public static void main(String&#x5B;] args) {\r\n   List&lt;Integer&gt; arrNumbers =  Arrays.asList(1,2,3,1,4,2);\r\n\r\n   System.out.println(&quot;Output 2&quot;);\r\n   arrNumbers.stream().sorted().forEach(System.out::println);\r\n}\r\n<\/pre>\n<p><strong>Output<\/strong><\/p>\n<pre>\r\nOutput 2\r\n1\r\n1\r\n2\r\n2\r\n3\r\n4\r\n<\/pre>\n<\/li>\n<li><strong>Sorting based on Natural Order, Reverse Order and Length of String?<\/strong>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npublic static void main(String&#x5B;] args) {\r\n        List&lt;String&gt; arrSkills = Arrays.asList(&quot;Java&quot;, &quot;Python&quot;,  &quot;Ant&quot;);\r\n\r\n        System.out.println(&quot;Sorting in Natural Order&quot;);\r\n        arrSkills.stream().sorted(Comparator.naturalOrder()).forEach(System.out::println);\r\n\r\n        System.out.println(&quot;Sorting in Reverse Order&quot;);\r\n        arrSkills.stream().sorted(Comparator.reverseOrder()).forEach(System.out::println);\r\n\r\n        System.out.println(&quot;Sorting based on Length&quot;);\r\n        arrSkills.stream().sorted(Comparator.comparing(str -&gt; str.length())).forEach(System.out::println);\r\n\r\n    }\r\n<\/pre>\n<p><strong>Output<\/strong><\/p>\n<pre>\r\nSorting in Natural Order\r\nAnt\r\nJava\r\nPython\r\nSorting in Reverse Order\r\nPython\r\nJava\r\nAnt\r\nSorting based on Length\r\nAnt\r\nJava\r\nPython\r\n<\/pre>\n<\/li>\n<li><strong>Collect the Elements in the List &#8211; Collect Even Nos and Length of String ?<\/strong>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\n    public static void main(String&#x5B;] args) {\r\n        List&lt;Integer&gt; arrNumbers = Arrays.asList(1,2,4,5,7,6);\r\n        List&lt;String&gt; arrNames = Arrays.asList(&quot;Java&quot;, &quot;Oracle&quot;, &quot;Maven&quot;, &quot;Ant&quot;);\r\n\r\n        System.out.println(&quot;Collect Even Nos in list&quot;);\r\n        List&lt;Integer&gt; arrEvenNumbers = arrNumbers.stream().filter(x-&gt;x%2==0).collect(Collectors.toList());\r\n        arrEvenNumbers.stream().forEach(System.out::println);\r\n\r\n        System.out.println(&quot;Length of Elements&quot;);\r\n        List&lt;Integer&gt; arrEvenNumbers2 = arrNames.stream().map(str-&gt;str.length()).collect(Collectors.toList());\r\n        arrEvenNumbers2.stream().forEach(System.out::println);\r\n    }\r\n<\/pre>\n<p><strong>Output<\/strong><\/p>\n<pre>\r\nCollect Even Nos in list\r\n2\r\n4\r\n6\r\nLength of Elements\r\n4\r\n6\r\n5\r\n3\r\n<\/pre>\n<\/li>\n<li><strong>Find the Sum of Nos in List?<\/strong>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\n<\/pre>\n<\/li>\n<li><strong>Find the Sum of Nos in List?<\/strong>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\n<\/pre>\n<\/li>\n<li><strong>Find the Sum of Nos in List?<\/strong>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\n<\/pre>\n<\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"<p>What is the Difference between Map and Filter? Both perform intermediate Operations and returns stream as output.By using map, you transform the object values. Map returns a stream consisting of the results of applying the given function to the elements of this stream. In a simple sentence, the map returns the transformed object value. Filter&hellip; <a href=\"https:\/\/codethataint.com\/blog\/stream-code-interview-questions\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[260],"tags":[],"class_list":["post-4258","post","type-post","status-publish","format-standard","hentry","category-interview-questions-java-8"],"_links":{"self":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/4258","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/comments?post=4258"}],"version-history":[{"count":5,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/4258\/revisions"}],"predecessor-version":[{"id":4274,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/4258\/revisions\/4274"}],"wp:attachment":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/media?parent=4258"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/categories?post=4258"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/tags?post=4258"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}