• Post Reply Bookmark Topic Watch Topic
  • New Topic

Lambda Expression: unchecked call to compareTo(T) as a member of the raw type Comparable

 
Charles Sexton
Ranch Hand
Posts: 273
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have the method below and I have been trying to solve the following warning: unchecked call to compareTo(T) as a member of the raw type Comparable. I can't figure out a way to solve this, any suggestions? I am still quite a nooby with generics and lambdas which is why I am studying and practicing.

 
Stephan van Hulst
Bartender
Posts: 6583
84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your field extractor's type contains a raw type argument.

Comparable is a generic type, so you should parameterize it.

It appears that the method doesn't know the type of the field that will be extracted from the book. At first it may seem reasonable to parameterize your Comparable with the unbounded wildcard:
Comparable<?>. This won't work however, because that means that the field extracted from the first book might not be comparable to the field extracted from the second book. That means that we do care about the type, we just don't know the exact type. The solution is to make the method itself generic:
 
Piet Souris
Rancher
Posts: 1641
36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can simplify and generalize this a little: list.sort(c), (assuming book.values() is a list-like object)
where c is a Comparator that may include the field extractor.
 
Campbell Ritchie
Marshal
Posts: 52587
119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you really mean Comparable? Are you sure it shou‍ldn't be Comparator?
 
Piet Souris
Rancher
Posts: 1641
36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OP's method used in 'sorted' is a comparator, even though compareTo is used.
 
Rob Spoor
Sheriff
Posts: 20822
68
Chrome Eclipse IDE Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Charles Sexton wrote:

FYI, that can be shorter:
It does exactly the same, but you hide it in the comparing method call.
 
Charles Sexton
Ranch Hand
Posts: 273
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Spoor wrote:
Charles Sexton wrote:

FYI, that can be shorter:
It does exactly the same, but you hide it in the comparing method call.


So this did work but now I have to cast it to List<Book>. Im not a big fan of casting and try to avoid it at all costs. I am currently trying to find a way without casting.

 
Campbell Ritchie
Marshal
Posts: 52587
119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Piet Souris wrote:OP's method used in 'sorted' is a comparator . . .
That is what I thought.

If you are having to cast the return type to List<Book>, that is most probably because your book map is not parametrised. You probably have a Map full stop whereas you would be better off with a Map<Something, Book>. Please show us how you are instantiating that Map. You are right to dislike casting.
 
Charles Sexton
Ranch Hand
Posts: 273
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you are having to cast the return type to List<Book>, that is most probably because your book map is not parametrised. You probably have a Map full stop whereas you would be better off with a Map<Something, Book>. Please show us how you are instantiating that Map. You are right to dislike casting.


 
Charles Sexton
Ranch Hand
Posts: 273
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I meant to qoute Campbell above ^^^^
 
Charles Sexton
Ranch Hand
Posts: 273
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Unit Tests

 
Campbell Ritchie
Marshal
Posts: 52587
119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try running the code on Eclipse and hover your mouse over each method call. You shou‍ld get a tooltip/popup with things like Collection<Book> or Stream<Book> written on. See whether the penultimate method call still has Stream<Book> and also what type the final method call shows.
 
Charles Sexton
Ranch Hand
Posts: 273
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hovering over I get:

hovering over in the call, I get

hovering over I get

hovering over I get

hovering over in I get
 
Stephan van Hulst
Bartender
Posts: 6583
84
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because you're still using raw types.

Comparable is a generic type. Don't use it without specifying type arguments.

Your compiler should actually warn you about this. Is your Eclipse setup to report compiler warnings? If so, fix your warnings.
 
Charles Sexton
Ranch Hand
Posts: 273
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
so I just added this to remove the warning


 
Campbell Ritchie
Marshal
Posts: 52587
119
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Charles Sexton wrote:so I just added this to remove the warning . . .
Nonononononononono! Don't use suppress warnings until you are absolutely sure you have got all the type right. And you haven't.
 
Charles Sexton
Ranch Hand
Posts: 273
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does this look good? I can't use any other type for comparable but object because I am comparing ints, doubles, strings....etc.... I might want to compare the price of a book.... or the quantity..... of course it wouldn't be int but I would have to do Integer.....


 
Charles Sexton
Ranch Hand
Posts: 273
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well I spoke too soon, in my unit tests now I get the following error which is pretty self explanatory....



occurs

 
Charles Sexton
Ranch Hand
Posts: 273
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

So this worked as stated above but I had no idea what was being said really. Now I do and I completely understand why and whats going on. Thanks to everyone......no @SupressedWarnings()   

 
Stephan van Hulst
Bartender
Posts: 6583
84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Great! Now that you get it, let's go back to Piet's comment.

Why does your method accept a field extractor, only to base a sorting order on, if you can just pass the method a Comparator? That's exactly what they're intended for in the first place:
 
Piet Souris
Rancher
Posts: 1641
36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And it is more general, since you can use book fields that are not Comparable (if any).
 
Charles Sexton
Ranch Hand
Posts: 273
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I wanted to use the Function Interface just for learning. Never really played with it or the Streams class. Actually didn't even know these existed until a few days ago.
 
Charles Sexton
Ranch Hand
Posts: 273
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Never used Comparator either. I have used comparable several times before.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!