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
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.
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.
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:
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...