Comparator

Maurizio Cimadamore maurizio.cimadamore at oracle.com
Mon Jul 15 09:18:44 PDT 2013


On 15/07/13 17:15, Marc Petit-Huguenin wrote:
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA256
>
> On 07/15/2013 09:09 AM, Maurizio Cimadamore wrote:
>> On 15/07/13 16:53, Marc Petit-Huguenin wrote: Hi,
>>
>> I am trying to use a comparator on a Map.Entry<String, Integer> stream, and
>> I am not sure why this does not compile:
>>
>> .sorted(Comparator.comparing(Map.Entry::getValue))
>>> This is as expected - the method reference Map.Entry is defined on a
>>> generic class with unknown parameter types (some Map<K, V>) - therefore
>>> the compiler needs the info from the target (in a top-down way) in order
>>> to decide what the method reference looks like. But the target type info
>>> is incomplete as it depends on inference variables on its own.
>>> So the compiler ends up applying a simpler (arity-based only) check
>>> during overload resolution - which means the compiler can't really pick
>>> the right comparing() method ahead of the target-type.
>>> A shorter way to get this to compile should be this:
>>> .sorted(Comparator.comparing(Map.Entry<String, Integer>::getValue))
> I forgot to tell that I tried this too.  It fails with a "error: reference to
> comparing is ambiguous", but not with the new "inference loop" error.
>
> Note that ".sorted(Comparator.comparing((Map.Entry<String, Integer> entry) ->
> entry.getValue()))" (i.e. without the .intValue()) fails with a similar error.
Are you using the official JDK 8 or the experimental lambda-repo 
compiler? Only the latter contains a fix for this.

Maurizio
>
>>
>>> Let me know if that works for you.
>>> Maurizio
>> error: reference to comparing is ambiguous error: incompatible types:
>> Cannot instantiate inference variables T because of an inference loop
>>
>> (interestingly the second error was not present when using a build from
>> last week)
>>
>> These two compile:
>>
>> .sorted(Comparator.comparing((ToIntFunction<Map.Entry<String,
>> Integer>>)Map.Entry::getValue))
>>
>> .sorted(Comparator.comparing((Map.Entry<String, Integer> entry) ->
>> entry.getValue().intValue()))
>>
> - -- 
> Marc Petit-Huguenin
> Email: marc at petit-huguenin.org
> Blog: http://blog.marc.petit-huguenin.org
> Profile: http://www.linkedin.com/in/petithug
> -----BEGIN PGP SIGNATURE-----
> Version: GnuPG v1.4.12 (GNU/Linux)
>
> iQIcBAEBCAAGBQJR5CAiAAoJECnERZXWan7EQn4QANE0y2m3MwC9WbPpxVcKAq26
> W++E2/IOtQhS324TdPjc6hI61WMZ1RbEZp74AP30loqBmNREE6UZa4kXtuqE6kPg
> 45GIzmSzoY54+9r76sG2ByrhTgQ2pw5lGjIdQln7TjWU1ZgOEr2vnfGtcyC1n3jM
> Oii2jlejkG04q+6oOUiEKxW+0jQXhr35puAQi/c1tpzemIfRgwRVYaHiHWiprvI7
> Suni2AlaQTNLckdLZIoX79p89SyuSpcQrs3ge978GAp5mdMx7BNRyPQeNpo3wKWk
> ZZnmTf7haF/U4+BWct6B8/Oi3lsj69t8hJR3rPXAUunl8Wm9r2u0+e1PDm6okgM0
> BdM/0K91h3/E3A+NcH38xrw12SHi+ngRim7iP+AU7Sldvo2P7P85diTm94fJbiIb
> GY5x9GAe3/D56X/AfTIi3t0vigepC3N/lxG3/7mD3haHxQMuZC/6hbdb5K7aqhES
> FQuj/swWA3Jxp9ww72RVppFht3So4XjcEZss7bVi3T2hhSINBlyWSJByjBkEq8NP
> 2jyqHazRvlxI17pUDxHcxXvNtsuhdW4qW6u6py3req5d9Q5ZwoDNGd71vRtTjB2d
> cSq5X/DSadlphKBbG0KjGvzHIlvHu/vmJg1tc09XDdejos64SKpY801vLDIi0R6i
> fIdXhGY8kYiLsmbLYiaN
> =Fuli
> -----END PGP SIGNATURE-----



More information about the lambda-dev mailing list