Comparator
Marc Petit-Huguenin
marc at petit-huguenin.org
Mon Jul 15 10:17:01 PDT 2013
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA256
On 07/15/2013 10:11 AM, Maurizio Cimadamore wrote:
> On 15/07/13 18:06, Marc Petit-Huguenin wrote: On 07/15/2013 09:16 AM, Henry
> Jen wrote:
>>>> Or use the new static method,
>>>>
>>>> .sorted(Map.Entry<String, Integer>.comparingByValue())
> Nice! But it did not compile as it:
>
> .sorted(Map.Entry.comparingByValue())
>> Does the above works? I think Henry pasted wrong snippet, as you can't
>> really pass type-argument to a receiver in a static method call.
Yes, "Map.Entry.comparingByValue()" works, but
"Map.Entry.comparingByValue().reverse()" does not, so I changed it to
".sorted(Map.Entry.<String, Integer>comparingByValue().reversed())"
>
>> Maurizio
>
> worked, but I needed to reverse the order, so I had to use
>
> .sorted(Map.Entry.<String, Integer>comparingByValue().reversed())
>
> I am a little bit worried that finding the right syntax relies on try and
> fail.
>
>>>> Cheers, Henry
>>>>
>>>> On Jul 15, 2013, at 9:09 AM, Maurizio Cimadamore
>>>> <maurizio.cimadamore at oracle.com> 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))
>>>>>>
>>>>>>
>>>>>> 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()))
>>>>
>>>> Thanks.
>>>>
>>>>>
> -- Marc Petit-Huguenin Email: marc at petit-huguenin.org Blog:
> http://blog.marc.petit-huguenin.org Profile:
> http://www.linkedin.com/in/petithug
>
- --
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)
iQIcBAEBCAAGBQJR5C59AAoJECnERZXWan7E8DgQAJZWgWKediHEVFdq2XYkQhoZ
o2g/JgODuaPLht4MndwTl4KdcfBB4bzN9ilFnixRYuNdFXKWbkPSCrqjq2/A3Vqe
dRo7UaKVQ3aK2d/YlBG/bYFv6tV6lZr+iDlLnmQ1jvh90/bj8CFPcDJgtP8dzUiU
ibEIf8pPG6h11ZWJ8Gyi0M5kSy0ZJTYxXR+PAvobP1Bhcc0naYScdoedueDWzxvC
EEJXLPPfCJX90gj2mMFnPMehqwFreTx2ZdjbBRsONyvNOS78rI1sTJXRjQi9Aq6c
kVoF2YM1cfH4G/0VcLCWkib3y9q2C6DIzB5URMt0TimJNiaeV+VGi5z9ja4+/ft1
xg2tgk2FNYk2rRYtnIj7OOeyRPU4cTqTF9diHdQd9ejwQecLlkxpMO6/svvHa+N9
UfqyDa0XspitOROBg5dBdKi0+UuBqvqMq2DaM7duLjz5WNi2Q1z41bgGfudWCYnD
JTaXJalFhVP8VraI5C/+BmuHYChd9v8TkH0KeBVdUdrjfpkISNCfHiTJ/jiSEvsB
nmIkBv5EBTj5ww7KtpYJ8ZKl6L8cP/1V5Bx5mgyrAREwT9O8CXLj279vU/MZfEMm
HmxgbsgyZYvuxlduylLnZnkUe4j0w3GghpX23px66sA9hYLEbRE3+kT0l2W8UA/H
UBwtrOoWSOcfJri36p8A
=HSVc
-----END PGP SIGNATURE-----
More information about the lambda-dev
mailing list