What is the type of Map.Entry.comparingByValue().reversed()? [duplicate]










0
















This question already has an answer here:



  • Generic type inference not working with method chaining?

    1 answer



I have a list of map entries



Map<String, Integer> map = new HashMap<>();
...(fill the map)...
List<Entry<String, Integer>> entries = new ArrayList<>(map.entrySet());


and I want to sort it by values according to this answer, but in reverse order. When I do



Comparator<Entry<String, Integer>> cmp = Entry.comparingByValue();
entries.sort(cmp.reversed());


everything works fine. But when I try to shorten the above two lines to



entries.sort(Entry.comparingByValue().reversed());


the compiler returns



error: incompatible types: Comparator<Entry<Object,V>> cannot be converted to Comparator<? super Entry<String,Integer>>
entries.sort(Entry.comparingByValue().reversed());
where V is a type-variable:
V extends Comparable<? super V>


This seems strange because the implementation of Comparator's reversed() default method simply transfers it to Collections.reverseOrder() and I can change the above line to



entries.sort(Collections.reverseOrder(Entry.comparingByValue()));


to work. But what is the correct type of Entry.comparingByValue().reversed()?



Comparator<Entry<String, Integer>> cmp = Entry.comparingByValue().reversed();


doesn't seem to work. Omitting the type parameter Entry<String, Integer> works, but this results in "unchecked method invocation" warning from the compiler later.










share|improve this question













marked as duplicate by Michael java
Users with the  java badge can single-handedly close java questions as duplicates and reopen them as needed.

StackExchange.ready(function()
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();

);
);
);
Nov 15 '18 at 18:39


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.













  • 2





    Check answers to this question: stackoverflow.com/questions/24794924/…

    – ernest_k
    Nov 15 '18 at 18:34











  • @ernest_k Well it seems like you are. I've marked my answer as community wiki just in case anyone thinks I closed this for any "competitive advantage".

    – Michael
    Nov 15 '18 at 18:41











  • Thanks @Michael

    – ernest_k
    Nov 15 '18 at 18:42















0
















This question already has an answer here:



  • Generic type inference not working with method chaining?

    1 answer



I have a list of map entries



Map<String, Integer> map = new HashMap<>();
...(fill the map)...
List<Entry<String, Integer>> entries = new ArrayList<>(map.entrySet());


and I want to sort it by values according to this answer, but in reverse order. When I do



Comparator<Entry<String, Integer>> cmp = Entry.comparingByValue();
entries.sort(cmp.reversed());


everything works fine. But when I try to shorten the above two lines to



entries.sort(Entry.comparingByValue().reversed());


the compiler returns



error: incompatible types: Comparator<Entry<Object,V>> cannot be converted to Comparator<? super Entry<String,Integer>>
entries.sort(Entry.comparingByValue().reversed());
where V is a type-variable:
V extends Comparable<? super V>


This seems strange because the implementation of Comparator's reversed() default method simply transfers it to Collections.reverseOrder() and I can change the above line to



entries.sort(Collections.reverseOrder(Entry.comparingByValue()));


to work. But what is the correct type of Entry.comparingByValue().reversed()?



Comparator<Entry<String, Integer>> cmp = Entry.comparingByValue().reversed();


doesn't seem to work. Omitting the type parameter Entry<String, Integer> works, but this results in "unchecked method invocation" warning from the compiler later.










share|improve this question













marked as duplicate by Michael java
Users with the  java badge can single-handedly close java questions as duplicates and reopen them as needed.

StackExchange.ready(function()
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();

);
);
);
Nov 15 '18 at 18:39


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.













  • 2





    Check answers to this question: stackoverflow.com/questions/24794924/…

    – ernest_k
    Nov 15 '18 at 18:34











  • @ernest_k Well it seems like you are. I've marked my answer as community wiki just in case anyone thinks I closed this for any "competitive advantage".

    – Michael
    Nov 15 '18 at 18:41











  • Thanks @Michael

    – ernest_k
    Nov 15 '18 at 18:42













0












0








0









This question already has an answer here:



  • Generic type inference not working with method chaining?

    1 answer



I have a list of map entries



Map<String, Integer> map = new HashMap<>();
...(fill the map)...
List<Entry<String, Integer>> entries = new ArrayList<>(map.entrySet());


and I want to sort it by values according to this answer, but in reverse order. When I do



Comparator<Entry<String, Integer>> cmp = Entry.comparingByValue();
entries.sort(cmp.reversed());


everything works fine. But when I try to shorten the above two lines to



entries.sort(Entry.comparingByValue().reversed());


the compiler returns



error: incompatible types: Comparator<Entry<Object,V>> cannot be converted to Comparator<? super Entry<String,Integer>>
entries.sort(Entry.comparingByValue().reversed());
where V is a type-variable:
V extends Comparable<? super V>


This seems strange because the implementation of Comparator's reversed() default method simply transfers it to Collections.reverseOrder() and I can change the above line to



entries.sort(Collections.reverseOrder(Entry.comparingByValue()));


to work. But what is the correct type of Entry.comparingByValue().reversed()?



Comparator<Entry<String, Integer>> cmp = Entry.comparingByValue().reversed();


doesn't seem to work. Omitting the type parameter Entry<String, Integer> works, but this results in "unchecked method invocation" warning from the compiler later.










share|improve this question















This question already has an answer here:



  • Generic type inference not working with method chaining?

    1 answer



I have a list of map entries



Map<String, Integer> map = new HashMap<>();
...(fill the map)...
List<Entry<String, Integer>> entries = new ArrayList<>(map.entrySet());


and I want to sort it by values according to this answer, but in reverse order. When I do



Comparator<Entry<String, Integer>> cmp = Entry.comparingByValue();
entries.sort(cmp.reversed());


everything works fine. But when I try to shorten the above two lines to



entries.sort(Entry.comparingByValue().reversed());


the compiler returns



error: incompatible types: Comparator<Entry<Object,V>> cannot be converted to Comparator<? super Entry<String,Integer>>
entries.sort(Entry.comparingByValue().reversed());
where V is a type-variable:
V extends Comparable<? super V>


This seems strange because the implementation of Comparator's reversed() default method simply transfers it to Collections.reverseOrder() and I can change the above line to



entries.sort(Collections.reverseOrder(Entry.comparingByValue()));


to work. But what is the correct type of Entry.comparingByValue().reversed()?



Comparator<Entry<String, Integer>> cmp = Entry.comparingByValue().reversed();


doesn't seem to work. Omitting the type parameter Entry<String, Integer> works, but this results in "unchecked method invocation" warning from the compiler later.





This question already has an answer here:



  • Generic type inference not working with method chaining?

    1 answer







java generics comparator






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 15 '18 at 18:20









John McClaneJohn McClane

1,5602421




1,5602421




marked as duplicate by Michael java
Users with the  java badge can single-handedly close java questions as duplicates and reopen them as needed.

StackExchange.ready(function()
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();

);
);
);
Nov 15 '18 at 18:39


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.









marked as duplicate by Michael java
Users with the  java badge can single-handedly close java questions as duplicates and reopen them as needed.

StackExchange.ready(function()
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();

);
);
);
Nov 15 '18 at 18:39


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.









  • 2





    Check answers to this question: stackoverflow.com/questions/24794924/…

    – ernest_k
    Nov 15 '18 at 18:34











  • @ernest_k Well it seems like you are. I've marked my answer as community wiki just in case anyone thinks I closed this for any "competitive advantage".

    – Michael
    Nov 15 '18 at 18:41











  • Thanks @Michael

    – ernest_k
    Nov 15 '18 at 18:42












  • 2





    Check answers to this question: stackoverflow.com/questions/24794924/…

    – ernest_k
    Nov 15 '18 at 18:34











  • @ernest_k Well it seems like you are. I've marked my answer as community wiki just in case anyone thinks I closed this for any "competitive advantage".

    – Michael
    Nov 15 '18 at 18:41











  • Thanks @Michael

    – ernest_k
    Nov 15 '18 at 18:42







2




2





Check answers to this question: stackoverflow.com/questions/24794924/…

– ernest_k
Nov 15 '18 at 18:34





Check answers to this question: stackoverflow.com/questions/24794924/…

– ernest_k
Nov 15 '18 at 18:34













@ernest_k Well it seems like you are. I've marked my answer as community wiki just in case anyone thinks I closed this for any "competitive advantage".

– Michael
Nov 15 '18 at 18:41





@ernest_k Well it seems like you are. I've marked my answer as community wiki just in case anyone thinks I closed this for any "competitive advantage".

– Michael
Nov 15 '18 at 18:41













Thanks @Michael

– ernest_k
Nov 15 '18 at 18:42





Thanks @Michael

– ernest_k
Nov 15 '18 at 18:42












2 Answers
2






active

oldest

votes


















3














The compiler is not clever enough to infer the generic type parameters in this case. You can specify them explicitly:



entries.sort(Entry.<String, Integer>comparingByValue().reversed());


The issue is that you first need the type of the comparator returned by comparingByValue to know the type of the one returned by reversed.



I don't see any good reason why a clever compiler couldn't work backwards and know that sort requires a particular type, so reversed must be a particular type, so comparingByValue must be a particular type. But that's not how things are.



Type inference is still being worked on (as recently as Java 10), so maybe in the future, who knows.






share|improve this answer
































    1














    When you shorten down



    Comparator<Entry<String, Integer>> cmp = Entry.comparingByValue();
    entries.sort(cmp.reversed());


    to



    entries.sort(Entry.comparingByValue().reversed());


    you remove the type information gleaned from cmp’s declaration. The compiler still needs to know that information, so you need to add the generic typing to Entry:



    entries.sort(Entry.<String, Integer>comparingByValue().reversed());





    share|improve this answer

























    • Oops, looks like I put the . in the wrong place. Fixed now.

      – MTCoster
      Nov 15 '18 at 18:41

















    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    3














    The compiler is not clever enough to infer the generic type parameters in this case. You can specify them explicitly:



    entries.sort(Entry.<String, Integer>comparingByValue().reversed());


    The issue is that you first need the type of the comparator returned by comparingByValue to know the type of the one returned by reversed.



    I don't see any good reason why a clever compiler couldn't work backwards and know that sort requires a particular type, so reversed must be a particular type, so comparingByValue must be a particular type. But that's not how things are.



    Type inference is still being worked on (as recently as Java 10), so maybe in the future, who knows.






    share|improve this answer





























      3














      The compiler is not clever enough to infer the generic type parameters in this case. You can specify them explicitly:



      entries.sort(Entry.<String, Integer>comparingByValue().reversed());


      The issue is that you first need the type of the comparator returned by comparingByValue to know the type of the one returned by reversed.



      I don't see any good reason why a clever compiler couldn't work backwards and know that sort requires a particular type, so reversed must be a particular type, so comparingByValue must be a particular type. But that's not how things are.



      Type inference is still being worked on (as recently as Java 10), so maybe in the future, who knows.






      share|improve this answer



























        3












        3








        3







        The compiler is not clever enough to infer the generic type parameters in this case. You can specify them explicitly:



        entries.sort(Entry.<String, Integer>comparingByValue().reversed());


        The issue is that you first need the type of the comparator returned by comparingByValue to know the type of the one returned by reversed.



        I don't see any good reason why a clever compiler couldn't work backwards and know that sort requires a particular type, so reversed must be a particular type, so comparingByValue must be a particular type. But that's not how things are.



        Type inference is still being worked on (as recently as Java 10), so maybe in the future, who knows.






        share|improve this answer















        The compiler is not clever enough to infer the generic type parameters in this case. You can specify them explicitly:



        entries.sort(Entry.<String, Integer>comparingByValue().reversed());


        The issue is that you first need the type of the comparator returned by comparingByValue to know the type of the one returned by reversed.



        I don't see any good reason why a clever compiler couldn't work backwards and know that sort requires a particular type, so reversed must be a particular type, so comparingByValue must be a particular type. But that's not how things are.



        Type inference is still being worked on (as recently as Java 10), so maybe in the future, who knows.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 15 '18 at 18:33


























        community wiki





        2 revs
        Michael
























            1














            When you shorten down



            Comparator<Entry<String, Integer>> cmp = Entry.comparingByValue();
            entries.sort(cmp.reversed());


            to



            entries.sort(Entry.comparingByValue().reversed());


            you remove the type information gleaned from cmp’s declaration. The compiler still needs to know that information, so you need to add the generic typing to Entry:



            entries.sort(Entry.<String, Integer>comparingByValue().reversed());





            share|improve this answer

























            • Oops, looks like I put the . in the wrong place. Fixed now.

              – MTCoster
              Nov 15 '18 at 18:41















            1














            When you shorten down



            Comparator<Entry<String, Integer>> cmp = Entry.comparingByValue();
            entries.sort(cmp.reversed());


            to



            entries.sort(Entry.comparingByValue().reversed());


            you remove the type information gleaned from cmp’s declaration. The compiler still needs to know that information, so you need to add the generic typing to Entry:



            entries.sort(Entry.<String, Integer>comparingByValue().reversed());





            share|improve this answer

























            • Oops, looks like I put the . in the wrong place. Fixed now.

              – MTCoster
              Nov 15 '18 at 18:41













            1












            1








            1







            When you shorten down



            Comparator<Entry<String, Integer>> cmp = Entry.comparingByValue();
            entries.sort(cmp.reversed());


            to



            entries.sort(Entry.comparingByValue().reversed());


            you remove the type information gleaned from cmp’s declaration. The compiler still needs to know that information, so you need to add the generic typing to Entry:



            entries.sort(Entry.<String, Integer>comparingByValue().reversed());





            share|improve this answer















            When you shorten down



            Comparator<Entry<String, Integer>> cmp = Entry.comparingByValue();
            entries.sort(cmp.reversed());


            to



            entries.sort(Entry.comparingByValue().reversed());


            you remove the type information gleaned from cmp’s declaration. The compiler still needs to know that information, so you need to add the generic typing to Entry:



            entries.sort(Entry.<String, Integer>comparingByValue().reversed());






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Nov 15 '18 at 18:41

























            answered Nov 15 '18 at 18:24









            MTCosterMTCoster

            3,85922141




            3,85922141












            • Oops, looks like I put the . in the wrong place. Fixed now.

              – MTCoster
              Nov 15 '18 at 18:41

















            • Oops, looks like I put the . in the wrong place. Fixed now.

              – MTCoster
              Nov 15 '18 at 18:41
















            Oops, looks like I put the . in the wrong place. Fixed now.

            – MTCoster
            Nov 15 '18 at 18:41





            Oops, looks like I put the . in the wrong place. Fixed now.

            – MTCoster
            Nov 15 '18 at 18:41



            這個網誌中的熱門文章

            How to read a connectionString WITH PROVIDER in .NET Core?

            In R, how to develop a multiplot heatmap.2 figure showing key labels successfully

            Museum of Modern and Contemporary Art of Trento and Rovereto