因为擦除你不能。您能做的最好的事情是在元组类中存储您计划让元组保存在“java.lang.Class”字段成员中的类型。然后您可以比较这些字段以确保元组类具有相同的类型。
另请参阅此线程:
Java 中 C++ Pair<L,R> 的等价物是什么?
如果您发布更多关于您的课程的信息,将会有所帮助。我认为未经检查的演员表和您等同的字段数意味着它应该是 Tuple<E,F> 不?
编辑:这是我经常使用的一个有用的 Pair 类(如果需要,你可以调整你的 Tuple 类)。注意,类似于其他人的建议,这个类只是让包含的成员决定平等问题。您的用例应该确定相等性是否真的基于所包含成员的类型。
/**
* Adapted from http://forums.sun.com/thread.jspa?threadID=5132045
*
*
* @author Tim Harsch
*
* @param <L>
* @param <R>
*/
public class Pair<L, R> {
private final L left;
private final R right;
public R getRight() {
return right;
} // end getter
public L getLeft() {
return left;
} // end getter
public Pair(final L left, final R right) {
this.left = left;
this.right = right;
} // end constructor
public static <A, B> Pair<A, B> create(A left, B right) {
return new Pair<A, B>(left, right);
} // end factory method
@Override
public final boolean equals(Object o) {
if (!(o instanceof Pair<?,?>))
return false;
final Pair<?, ?> other = (Pair<?, ?>) o;
return equal(getLeft(), other.getLeft()) && equal(getRight(), other.getRight());
} // end method
public static final boolean equal(Object o1, Object o2) {
if (o1 == null) {
return o2 == null;
}
return o1.equals(o2);
} // end method
@Override
public int hashCode() {
int hLeft = getLeft() == null ? 0 : getLeft().hashCode();
int hRight = getRight() == null ? 0 : getRight().hashCode();
return hLeft + (37 * hRight);
} // end method
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append('<');
if( left == null ) {
sb.append("null");
} else {
sb.append(left.toString());
} // end if
sb.append(',');
if( right == null ) {
sb.append("null");
} else {
sb.append(right.toString());
} // end if
sb.append('>');
return sb.toString();
} // end method
} // end class