<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Gerald AJ on Medium]]></title>
        <description><![CDATA[Stories by Gerald AJ on Medium]]></description>
        <link>https://medium.com/@geraldamalraj1985?source=rss-12cb054cb7f8------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*cNk8smjVHWhyQlh4_DWwzw.jpeg</url>
            <title>Stories by Gerald AJ on Medium</title>
            <link>https://medium.com/@geraldamalraj1985?source=rss-12cb054cb7f8------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 28 Nov 2026 03:53:47 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@geraldamalraj1985/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Java 8 Streams Interview Questions asked in Product companies — Part 1]]></title>
            <link>https://medium.com/@geraldamalraj1985/java-8-streams-interview-questions-asked-in-product-companies-a61df21b4c46?source=rss-12cb054cb7f8------2</link>
            <guid isPermaLink="false">https://medium.com/p/a61df21b4c46</guid>
            <category><![CDATA[java8-interview-questions]]></category>
            <category><![CDATA[product-development]]></category>
            <category><![CDATA[java-8-feature]]></category>
            <category><![CDATA[java8]]></category>
            <category><![CDATA[java8-streams]]></category>
            <dc:creator><![CDATA[Gerald AJ]]></dc:creator>
            <pubDate>Tue, 26 Nov 2024 12:36:39 GMT</pubDate>
            <atom:updated>2024-11-26T13:16:49.449Z</atom:updated>
            <content:encoded><![CDATA[<p><strong>Java 8 Streams Interview Questions asked in Product companies — Part 1</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/560/1*nRh4bBh3EQGAV4z2o_y5Ow.jpeg" /></figure><p>Now-a-days Java streams are becoming more important if you&#39;re are Java developer. In every technical discussion be it a product companies or service based companies you can expect at least 3 to 4 scenarios to be coded using Java 8 streams. The below examples are some of the scenarios I have faced in my technical interviews. I’m putting forth those examples in sequential part of episodes.</p><p>Let’s Begin!</p><blockquote><strong>Question 1 : How do you sort HashMap with keys and values.</strong></blockquote><pre>  Map&lt;String, Integer&gt; mapObj = new HashMap&lt;String, Integer&gt;();<br>  mapObj.put(&quot;i&quot;, 0);<br>  mapObj.put(&quot;am&quot;, 3);<br>  mapObj.put(&quot;zack&quot;, 0);<br>  mapObj.put(&quot;yigritte&quot;, 2);<br><br>  // Sort By values<br>  Map mapObj2 = (Map) mapObj.entrySet().stream().sorted(Map.Entry.comparingByValue()).collect(Collectors<br>    .toMap(Map.Entry::getKey, Map.Entry::getValue, (oldVal, newVal) -&gt; oldVal, LinkedHashMap::new));<br>  Integer[] finalResult = (Integer[]) mapObj2.values().toArray(new Integer[mapObj2.size()]);<br>  System.out.println(&quot;********************************* &quot; + Arrays.toString(finalResult));<br><br>  // Sort by keys<br>  Map mapObjSorted = mapObj.entrySet().stream().sorted(Map.Entry.comparingByKey()).collect(Collectors<br>    .toMap(Map.Entry::getKey, Map.Entry::getValue, (oldVal, newVal) -&gt; oldVal, LinkedHashMap::new));<br>  String[] result = (String[]) mapObjSorted.keySet().toArray(new String[mapObjSorted.size()]);<br>  System.out.println(&quot;********************************* &quot; + Arrays.toString(result));</pre><blockquote><strong>Question 1 (Follow up question) : If keys contains strings starts with uppercase letters.</strong></blockquote><pre>  Map&lt;String, Integer&gt; mapObj = new HashMap&lt;String, Integer&gt;();<br>  mapObj.put(&quot;I&quot;, 0);<br>  mapObj.put(&quot;am&quot;, 3);<br>  mapObj.put(&quot;Zack&quot;, 0);<br>  mapObj.put(&quot;Yigritte&quot;, 2);<br><br>  // Sort by keys<br>  Map mapObjSorted = mapObj.entrySet().stream().sorted(Map.Entry.comparingByKey(String.CASE_INSENSITIVE_ORDER)).collect(Collectors<br>    .toMap(Map.Entry::getKey, Map.Entry::getValue, (oldVal, newVal) -&gt; oldVal, LinkedHashMap::new));<br>  String[] result = (String[]) mapObjSorted.keySet().toArray(new String[mapObjSorted.size()]);<br>  System.out.println(&quot;********************************* &quot; + Arrays.toString(result));</pre><blockquote><strong>Question 2 : To find the first non-repeating character</strong></blockquote><pre>  String inputStr = &quot;solutions&quot;;<br>  String reslt = Arrays.stream(inputStr.split(&quot;&quot;)).filter(e -&gt; (inputStr.indexOf(e) == inputStr.lastIndexOf(e))).findFirst().get();<br>  System.out.println(&quot;--&gt; &quot; + reslt);</pre><blockquote><strong>Question 3 : Return the list in Integer and in descending order.</strong></blockquote><pre>  String[] lstOfElemnts = {&quot;1&quot;,&quot;11&quot;,&quot;111&quot;,&quot;1111&quot;,&quot;13&quot;};<br>  List&lt;Integer&gt; output = Arrays.stream(lstOfElemnts).map(Integer::parseInt).sorted(Comparator.reverseOrder()).collect(Collectors.toList());<br>  System.out.println(&quot;--&gt; &quot; + output);</pre><blockquote><strong>Question 4 : Find the non-repeating letter in the sentence</strong></blockquote><pre>String inptStr = &quot;today is friday&quot;;<br>Arrays.stream(inptStr.split(&quot;&quot;)).filter(x -&gt; inptStr.indexOf(x) == inptStr.lastIndexOf(x)).forEach(System.out::println);</pre><blockquote><strong>Question 5 : Find the non-repeating elements from list</strong></blockquote><pre>List&lt;String&gt; nonRepeatListEle = List.of(&quot;cat&quot;,&quot;dog&quot;,&quot;lion&quot;,&quot;zebra&quot;,&quot;cat&quot;,&quot;dog&quot;,&quot;giraffe&quot;,&quot;zebra&quot;,&quot;Hippo&quot;);<br><br>//First Approach<br>nonRepeatListEle.stream().filter(i -&gt; nonRepeatListEle.indexOf(i)== nonRepeatListEle.lastIndexOf(i)).forEach(System.out::println);<br><br>//Second Approach<br>nonRepeatListEle.stream().filter(i-&gt; Collections.frequency(nonRepeatListEle, i) == 1).collect(Collectors.toList()).forEach(System.out::println);</pre><blockquote><strong>Question 6 : Find the duplicate letters in the string</strong></blockquote><pre>String str = &quot;today is friday&quot;;<br>Arrays.stream(str.split(&quot;&quot;)).filter(x -&gt; Collections.frequency(List.of(str.split(&quot;&quot;)), x)&gt;1).collect(Collectors.toSet()).forEach(System.out::println);</pre><blockquote><strong>Question 7 : Find common elements from 2 different array</strong></blockquote><pre>  Integer[] a = { 1, 2, 3, 4, 5, 6 };<br>  Integer[] b = { 4, 1, 6, 9, 11 };<br><br>  //First Approach<br>  List&lt;Integer&gt; finalResult = Arrays.stream(a).filter(List.of(b)::contains).collect(Collectors.toList());<br>  System.out.println(&quot;=========================== &quot; + finalResult);<br><br>  //Second Approach<br>  Set&lt;Integer&gt; setObj = new HashSet&lt;Integer&gt;();<br>  Stream.concat(Arrays.stream(a),Arrays.stream(b)).filter(x -&gt; !setObj.add(x)).forEach(System.out::println);</pre><blockquote><strong>Question 8 : Flatten two arrays into one.</strong></blockquote><pre>  Integer[] a = {1,2,3,4,5,6,};<br>  Integer[] b = {4,1,6,9,11};<br><br>  List&lt;Integer&gt; lstOfObj =Stream.of(a,b).flatMap(Stream::of).collect(Collectors.toList());<br>  var arrayObj = Stream.of(a,b).flatMap(Stream::of).distinct().toArray();<br>  System.out.println(&quot;------------ &quot;+Arrays.toString(arrayObj));</pre><blockquote><strong>Question 9 : Retrieve the distinct cities of an employee over age 17</strong></blockquote><pre>public class Employee {<br><br> int id;<br> String name;<br> int age;<br> List&lt;Addresses&gt; address;<br><br> public Employee(int id, String name, int age, List&lt;Addresses&gt; address) {<br>  this.id = id;<br>  this.name = name;<br>  this.age = age;<br>  this.address = address;<br> }<br> <br> //getters and Setters<br>}<br><br>List&lt;Employee&gt; employee = List.of(new Employee(3, &quot;Alice&quot;, 25, List.of(new Addresses(&quot;India&quot;), new Addresses(&quot;Paris&quot;),new Addresses(&quot;Thailand&quot;))),<br>       new Employee(5, &quot;Barney&quot;, 17, List.of(new Addresses(&quot;Africa&quot;), new Addresses(&quot;London&quot;))),<br>       new Employee(5, &quot;Hans&quot;, 21, List.of(new Addresses(&quot;Thailand&quot;), new Addresses(&quot;Srilanka&quot;),new Addresses(&quot;India&quot;))));<br>  <br>employee.stream().filter(e -&gt; e.getAge()&gt;=18).flatMap(t -&gt; t.getAddress().stream().map(r -&gt; r.getCity())).distinct().forEach(System.out::println);</pre><blockquote><strong>Question 10 : Find the duplicates</strong></blockquote><pre>List&lt;Integer&gt; employeeIds = Arrays.asList(11, 12, 13, 14, 15,11,18, 16, 17, 18, 19, 20);<br>employeeIds.stream().filter(e -&gt; Collections.frequency(employeeIds, e) &gt;1).collect(Collectors.toSet()).forEach(System.out::println);</pre><p>That’s all Folks!</p><p>If you found my post useful, please consider giving it claps and sharing it with your friends and colleagues.</p><p>See you in next post!</p><p>~ AJ</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a61df21b4c46" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>