comparing and thenComparing gives compile error
<p>I am trying to sort <code>List</code> of employees by name then age using Java8 <code>Comparator</code>, I have created below <code>Comparator</code> but it gives me a compiler error </p>

<blockquote>
  <p><code>Type mismatch: cannot convert from Comparator&lt;Object&gt; to &lt;unknown&gt;</code></p>
</blockquote>

<pre><code>    Comparator&lt;String&gt; c = Comparator.comparing(s -&gt; s.split("\\s+")[0])
            .thenComparingInt(s -&gt; Integer.parseInt(s.split("\\s+")[1])); //compile error
</code></pre>

<p>but it works if I explicitly specify the Type</p>

<pre><code>    Comparator&lt;String&gt; c = Comparator.&lt;String, String&gt; comparing(s -&gt; s.split("\\s+")[0])
            .thenComparingInt(s -&gt; Integer.parseInt(s.split("\\s+")[1])); //works
</code></pre>

<p>or by creating two <code>Compartor</code>s and chain</p>

<pre><code>    Comparator&lt;String&gt; name = Comparator.comparing(s -&gt; s.split("\\s+")[0]);
    Comparator&lt;String&gt; age = Comparator.comparingInt(s -&gt; Integer.parseInt(s.split("\\s+")[1]));
    Comparator&lt;String&gt; cc = name.thenComparing(age); //works
</code></pre>

<p>I have specified the type <code>Comparator&lt;String&gt;</code> on the left side but why auto type inference is not finding the correct Type and expecting to specify explicitly.</p>

<p>Could someone clarify on this?</p>

<p>Here is the code</p>

<pre><code>    String[] arr = { "alan 25", "mario 30", "alan 19", "mario 25" };
    Comparator&lt;String&gt; c = Comparator.&lt;String, String&gt; comparing(s -&gt; s.split("\\s+")[0])
            .thenComparingInt(s -&gt; Integer.parseInt(s.split("\\s+")[1]));
    List&lt;String&gt; sorted = Arrays.stream(arr).sorted(c).collect(Collectors.toList());
    System.out.println(sorted);
</code></pre>

<p>output</p>

<pre><code>[alan 19, alan 25, mario 25, mario 30]
</code></pre>