• Post Reply Bookmark Topic Watch Topic
  • New Topic

compiler error with Comparable interface and Collections.sort  RSS feed

 
Srini Jayaraman
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm preparing for OCP SE 8 with Study guide book by Jeanne/Scott and facing an issue while compiling the code below:
Can anyone suggest how to resolve this compiler error. Thanks

compiler error:
********************************************************************************
C:\myApplication>javac Duck.java
Duck.java:17: error: cannot infer type arguments for Comparator<T>
        Comparator<Duck> byWeight = new Comparator<>(){
                                                  ^
  reason: cannot use '<>' with anonymous inner classes
  where T is a type-variable:
    T extends Object declared in interface Comparator
Duck.java:27: error: no suitable method found for sort(List<Duck>)
        Collections.sort(ducks);
                   ^
    method Collections.<T#1>sort(List<T#1>) is not applicable
      (inference variable T#1 has incompatible bounds
        equality constraints: Duck
        upper bounds: Comparable<? super T#1>)
    method Collections.<T#2>sort(List<T#2>,Comparator<? super T#2>) is not appli
cable
      (cannot infer type-variable(s) T#2
        (actual and formal argument lists differ in length))
  where T#1,T#2 are type-variables:
    T#1 extends Comparable<? super T#1> declared in method <T#1>sort(List<T#1>)
    T#2 extends Object declared in method <T#2>sort(List<T#2>,Comparator<? super
T#2>)
2 errors
********************************************************************************

 
D Nobels
Greenhorn
Posts: 15
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Line 17, you forgot the type at the instantiation of the comparator.

new Comparator<> becomes new Comparator<Duck>
 
Liutauras Vilda
Sheriff
Posts: 4914
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
At line 17, the type cannot be infered within diamond operator:
So this actually needs read as:

All that stuff, hower, could be replaced with lambda expression:

 
Srini Jayaraman
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your response -

I got rid of the first error at line 17

Can you suggest for the error on line 27  if possible. Thanks again
 
D Nobels
Greenhorn
Posts: 15
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Srini Jayaraman wrote:

Can you suggest for the error on line 27  if possible. Thanks again


Srini,

I ran your code and I don't have any error on line 27.

Here is the output of your code:


[puddle, quack]
[quack, puddle]
 
Liutauras Vilda
Sheriff
Posts: 4914
334
BSD
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try to get rid off the habit asking others to debug for you.

Have you fixed your first error you have been pointed out?
In case yes - there are no other compilation errors in your specified code.

So all that should compile and run. At least it does on my machine.
 
Srini Jayaraman
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Yes, it runs fine for me without any issues in IDE but getting an error on line 27 when i compile this using command prompt by saving this code in a notepad file.(i should have mentioned this earlier) I'm currently using notepad & compiling from command prompt. May be i'm missing something but will look into this more & update on this.


Thanks for your support.
 
Campbell Ritchie
Marshal
Posts: 56518
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Srini Jayaraman wrote: . . . runs fine for me without any issues in IDE but getting an error on line 27 when i compile this using command prompt by saving this code in a notepad file. . . .
What sort of new errors are you getting? If the code gets to the javac tool at all, you must get the same errors from the IDE as from the command line; if there are any differences, javac must be receiving different code.
But there is no such thing as a notepad file; there are only text files. It is possible that notepad is doing something to your encoding, or other things about the format of the file, which are causing your problems. Stop using notepad; it isn't good for programming. Try NotePad2 or NotePad++ instead (I prefer NotePad++ myself, though the two are similar).
 
Srini Jayaraman
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Campbell!!

I will start dubugging with IDE moving forward and do have Notepad++ as well as i don't see any issues in IDE with the same code.

 
Campbell Ritchie
Marshal
Posts: 56518
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try opening the code with NotePad++ and see whether you can compile it from that. Show us any new errors.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!