Comparator

Marc Petit-Huguenin marc at petit-huguenin.org
Mon Jul 15 10:06:19 PDT 2013


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA256

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())

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
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.12 (GNU/Linux)

iQIcBAEBCAAGBQJR5CwIAAoJECnERZXWan7ECPoP/1rpw5hbSz/v/YfGNC/ssgVG
aLfURFuciL9IBsUwPBt24p0ctbSWBC76NwplAipOuypl6GeFW+CfCVfqQWhBS6vs
RdOp9CU73Ql5syf/o/6k/djBGwskDvIWLQ8MXjmbas+5oxc1n7LW/KvPEgmGeagn
I9f2/WYfny0ErRKOrEcosaSJ0AwoddIy2RvAxW/KQpETAigxFYg5rZ+a6oQgPTaj
kBlUqWEOHajLPww1KxGWeV5wJpakn1SOFMC1QL8mn/+ZNAM7wE2srBG55cQ1xUUO
ZOlIIuj7De+ubMZeC3pJer7vWXZ64FFp7FTbVRqLxeTFxm6qyNd8VnPjEu3pdBhL
ibPYQRd06nE6n3+0Igb+01LeuY8FXwAv8EcCxowYllJZfAopPnPTkyUXmYk35BsL
iKj4jwt+Vs6Y1AxN5GNlOC0oaKv+Y5+LXezO/cOy7qMXEhX9vBN+YJnFkepmQz8H
UUKgV364PacPrKbFAgFZ3MtxI3UJv9aUNW5Z69xUOPuIjHC06WwXivrHSTszjLjw
E9GkM/QbUC5rf50/i+UfEET5+ujkz1YT2WwDmawNVxtPKqEMEtHBC4vUFj/1y2Te
v4xKWXfeTCTB+dMbk3K3qnwoVBaGZt+vK8OPQL+g8De/J2QTgq0sfop9U5CZxBqQ
wuMjkcrh6y67hKLaxfLj
=lnOv
-----END PGP SIGNATURE-----


More information about the lambda-dev mailing list