I've got a class using two generic parameters in its constructor. I would like to implement a method to compare two instances of this class, as done in the code below.
It works quite well except when one of the parameter is an arrays. I've read different topics about how to compare arrays with java.util.Arrays.equals(Object[] a, Object[] a2)
but as my parameters are not defined as arrays, I can't use this method.
How could return "true" in the content of my array is the same? Or is it possible to cast my parameters to use java.util.Arrays.equals(Object[] a, Object[] a2)
?
public class Pair<U, V> {
public final U first;
public final V second;
public Pair(U first, V second) {
this.first = first;
this.second = second;
}
@Override
public boolean equals(Object o) {
if (this == o)
return true;
if (o == null || getClass() != o.getClass())
return false;
Pair<?, ?> myPair= (Pair<?, ?>) o;
if (!first.equals(myPair.first) || !second.equals(myPair.second))
return false;
return true;
}
}