title: Java generic merge
body: <p>I wrote a merge function that merges two sorted lists into a single sorted list. The followings are the scenario for the code.</p>
  
  <ol>
  <li>Merging the lists [2, 4, 6], [3, 5, 6, 7] produces [2, 3, 4, 5, 6, 6, 7].</li>
  <li>Merging the lists [“Alice”, “Tom”], [“Bob”, “Richard”] produces [“Alice”, “Bob”, “Richard”, “Tom”].</li>
  <li>Merging the lists [2.3, 4.5], [2,5] produces [2, 2.3, 4.5, 5].</li>
  <li><p>Merging the lists [“A”, “XYZ”, “AXTU”] and [2, 4, 6] (where the first list is sorted by word length, and in the merge operation, if a string length in the first list is the same as a number in the second list, the string comes first) produces [“A”, 2, “XYZ”, “AXTU”, 4, 6];</p>
  
  <pre><code> public static void testCombine() {
      ArrayList&lt;ArrayList&lt;String&gt;&gt; mainList = new ArrayList&lt;&gt;();
      ArrayList&lt;String&gt; list1 = new ArrayList&lt;String&gt;(Arrays.asList("Alice", "Tom"));
      ArrayList&lt;String&gt; list2 = new ArrayList&lt;String&gt;(Arrays.asList("Bob", "Richard"));
      mainList.add(list1);
      mainList.add(list2);
      System.out.println(combine(mainList.stream()));
  }
  
  private static &lt;T extends Comparable&lt;? super T&gt;&gt; ArrayList&lt;T&gt; combine(Stream&lt;ArrayList&lt;T&gt;&gt; stream) {
      return stream.reduce((x, y) -&gt; {
          x.addAll(y);
          Collections.sort(x);
          return x;
      }).get();
  }
  </code></pre></li>
  </ol>
  
  <p><em>But, I was not getting the result for 3 and 4 types. I have to Implement the most general possible merge function, assuming that input lists are in sorted order (according to a natural or a specified ordering)</em></p>