• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Generics Warnings

 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Over the years we've had a lot of questions that I read as a need to compare two lists of things to see which things match or not. I thought I needed a generic solution and wrote the following. It gets two warnings on generics.

The Eclipse warnings are:

Type safety: The expression of type Iterator[] needs unchecked conversion to conform to Iterator<T>[]

Type safety: The method compareTo(Object) belongs to the raw type Comparable. References to generic type Comparable<T> should be parameterized

What does it take to make those go away? Besides @suppresswarnings.
 
Paul Clapham
Sheriff
Posts: 20983
31
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't have a Java 5 compiler here but the standard idiom for parameter types that are comparable iswhich is slightly different from what you have there. When you use this, implementing Comparable<T> requires writing a compareTo(T) method instead of a compareTo(Object) method.
 
Nitesh Kant
Bartender
Posts: 1638
IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Firstly, agreeing with what Paul said.
You signature should be


rather than



This would map the type T that you have in your class to the type that Comparable takes.

Now, in order to avoid warnings, you have to do the following:

1) Change

to


Got through the tutorial at http://java.sun.com/j2se/5/pdf/generics-tutorial.pdf
to understand the above.

2) Change



to



This will again give you a warning of unchecked cast though
I dont know a way where you can avoid the above. Let me know if you get one.
But yeah you will avoid an unchecked call to compareTo with the above. So, atleast your code is safe at runtime.

Hope this helps,
nitesh
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Putting Object[] in these lines still gave warnings, plus class cast exceptions from my unit test.

This works, but still has the same warnings:

But this line no longer has a warning

Net gain of one less warning.

For grins, here's a fragment of the tests ...
 
Alan Moore
Ranch Hand
Posts: 262
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Generics and arrays don't play well together. In this case, there are only two collections to compare, so you can store each one in its own variable. If you're working with an unknown number of collections, you should store them in one of the Collection classes. I don't get any warnings from this version:
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I got rid of the arrays in a different way ... but not the warnings.

Looks like you got them all. Sigh. Generics are a beast.
[ March 01, 2007: Message edited by: Stan James ]
 
Nitesh Kant
Bartender
Posts: 1638
IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oops sorry about the last post. I missed a few minor points and did not run the sample. (Not well versed with generics, just tried something, should have done more research)
Yeah, the best way is to get rid of the arrays as they do not really work well with generics.
Sure generics is a beast...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic