wood burning stoves 2.0*
The moose likes Java in General and the fly likes Generics Warnings Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Generics Warnings" Watch "Generics Warnings" New topic
Author

Generics Warnings

Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
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.


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18125
    
    8

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

Joined: Feb 25, 2007
Posts: 1638

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


apigee, a better way to API!
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
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

Joined: May 06, 2004
Posts: 262
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

Joined: Jan 29, 2003
Posts: 8791
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

Joined: Feb 25, 2007
Posts: 1638

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...
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Generics Warnings
 
Similar Threads
sorting arraylist of arraylist
ArrayList with Iterator new*
classic dictionary / hash table issue
My LinkedList Iterator - Can't print first element - No use of Java API
Rant about java Iterator interface