Re: Error in the equal method of generic Pair class
On Mon, 28 Jul 2008, puzzlecracker wrote:
public class Pair <T, U>
public boolean equals( Object o )
if ( this == o )
if ( o == null || !(getClass().isInstance( o )) )
Pair<T, U> other = getClass().cast(o);
return (first == null? other.first == null : first.equals( other.first ))
&& (second == null? other.second == null : second.equals( other.second ));
This line gives a cast error during compilation: Pair<T, U> other =
Why can't we use a traditional Pair<T, U> other =(Pair<T, U> ) o; ?
Because there's no way to actually check that cast. The way java does
generics is through a half-arsed approach called "type erasure", which
means all the stuff to do with generic types is done at compile time, and
none of the information survives to runtime. What looks like a
Pair<String, Date> to the compiler is a Pair<Object, Object> to the JVM.
Seriously. Java will only let you do casts it can actually check , so
it won't let you do this one.
So, what can you do?
You could cast to Pair<T, U>, suppress the warning, and hope that equals
never gets called with a different kind of pair. If it does, that cast
will still pass, but you'll get a random ClassCastException later on.
You could cast to Pair, without type parameters, and suppress the warning
about it being araw type.
You could cast to Pair<Object, Object>, and suppress the warning about the
unchecked cast, safe in the knowledge that this will always work.
Or, you could enter the voodoo wonderland of expert generics, and cast to
Pair<?, ?>. That means a Pair that could have any types as parameters, so
it compiles without warnings (at least, it does under Eclipse). And,
because you're never doing anything with its elements that requires them
to be of any specific type, it should work fine with the rest of your
 Apart from with arrays, kind of.
The glass is twice as big as it needs to be.