• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

Comparator - comparing() & reversed()

 
Ranch Hand
Posts: 127
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


On line 21: I try to reverse my comparator.  But I get compile error when calling a chained Comparator reversed() method.

Java needs me to cast it to Test type in the lambda expression as follows line #24:

Comparator<Integer> comparator = Comparator.comparing( (Test) x -> x.getNum() ).reversed();







Compile error output when I call chained Comparator reversed() on line #21.
 
Sheriff
Posts: 28346
97
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You need to decide whether you want a Comparator<Integer> or a Comparator<Test>. Your posted code uses the former but your error messages are based on code which uses the latter.

(Hint: to sort a List<Integer> you need a Comparator<Integer>.)

So once you have that straightened out, could you post the corrected code and the error messages which it produces?

 
Marshal
Posts: 80099
413
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If the field is an int, wouldn't you use comparingInt()?
 
henry leu
Ranch Hand
Posts: 127
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paul Clapham wrote:You need to decide whether you want a Comparator<Integer> or a Comparator<Test>. Your posted code uses the former but your error messages are based on code which uses the latter.

(Hint: to sort a List<Integer> you need a Comparator<Integer>.)

So once you have that straightened out, could you post the corrected code and the error messages which it produces?



Hi Paul,

I just updated  my code. It should be Comparator<Test>, not Comparator<Integer>. Sorry, that was a typo.

This is what I thought of last night. On line 21, the chained reversed() is too deep down the chain method call. x-> x.getNum() needs to know the type Test in order to call x.getNum()


                    Comparator<Test> comparator = Comparator.comparing( (Test) x -> x.getNum() ).reversed();

However, without the chained method call reversed(),

                    Comparator<Test> comparator = Comparator.comparing(  x -> x.getNum()); // compile ok

This line alone can inferred the Comparator<Test> from the target assignment.

Let me know whether or not my understanding is valid.

Thanks!

 
Campbell Ritchie
Marshal
Posts: 80099
413
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please don't edit posts after they have been replied to; that makes the replies look incorrect I have reversed the edit.
 
Paul Clapham
Sheriff
Posts: 28346
97
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

henry leu wrote:I just updated  my code. It should be Comparator<Test>, not Comparator<Integer>. Sorry, that was a typo.



I don't understand why it should be that. After all you have a List<Integer> so there's no point in creating a Comparator<Test>.

As for your three lines of code:



Only the first of the three lines compiles for me. When I look at the tutorials for how to do reversed Comparators like that, what I see them doing is this:



Which compiles successfully.

However as I said, none of those three Comparators can be used in your Collections.sort line of code.
 
Slideshow boring ... losing consciousness ... just gonna take a quick nap on this tiny ad ...
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic