Java 中泛型 class 的比较器
Comparator of generic class in Java
我想让下面的东西起作用:我有一个通用的 class public class TypeType<T1,T2>
,它有两个字段 public T1 t1
和 public T2 t2
。我想为此 class、compareFirst()
和 compareSecond()
编写两个 public static Comparator
方法。我希望第一种方法仅基于 t1
s 进行比较,而第二种方法仅基于 t2
s。第一种方法产生有效输出 if T1 implements Comparable<T1>
和第二种 if T2 implements Comparable<T2>
.
这样的事情可能吗?有没有办法检查 T1
和 T2
是否实现了 Comparable
?如果没有,我还能做这个工作吗? (此 class 仅供内部使用。)
非常感谢!
public class TypeType<T1,T2> {
public T1 t1;
public T2 t2;
public TypeType() {
this(null, null);
}
public TypeType(T1 t1, T2 t2) {
this.t1 = t1;
this.t2 = t2;
}
/*
* I know that the following does not work.
* This is just pseudo-code for what I would like to achieve.
*/
public static Comparator<TypeType<?,?>> comparatorFirst() {
return new Comparator<TypeType<?,?>> {
public int compare(TypeType<?,?> tt1, TypeType<?,?> tt2) {
return tt1.t1.compareTo(tt2.t1);
}
}
}
}
以防其他人感兴趣:我最终使用 Lambda 表达式实现了@pbabcdefp 的 Java 8 解决方案。
public static <T1 extends Comparable<? super T1>> Comparator<TypeType<T1,?>> compareOne() {
return (tt1, tt2) -> tt1.t1.compareTo(tt2.t1);
}
public static <T2 extends Comparable<? super T2>> Comparator<TypeType<?,T2>> compareTwo() {
return (tt1, tt2) -> tt1.t2.compareTo(tt2.t2);
}
public static <T1 extends Comparable<? super T1>,T2 extends Comparable<? super T2>> Comparator<TypeType<T1,T2>> compareOneThenTwo() {
return (tt1, tt2) -> {
if ( tt1.t1.equals(tt2.t1) )
return tt1.t2.compareTo(tt2.t2);
else
return tt1.t1.compareTo(tt2.t1);
};
}
public static <T1 extends Comparable<? super T1>,T2 extends Comparable<? super T2>> Comparator<TypeType<T1,T2>> compareTwoThenOne() {
return (tt1, tt2) -> {
if ( tt1.t2.equals(tt2.t2) )
return tt1.t1.compareTo(tt2.t1);
else
return tt1.t2.compareTo(tt2.t2);
};
}
instanceof
运算符可用于查看特定 class 是否派生自 class 或 class 是否实现特定接口。
即t1 instanceof Comparable
会 return true
如果它实现 Comparable
.
请注意泛型在运行时不存在,因此您只能检查是否实现了 Comparable
,而不是它实现了 Comparable<T1>
.
public static Comparator<TypeType<T1 extends Comparable<T1>,T1 extends Comparable<T2>>> comparatorFirst() {
return new Comparator<> {
public int compare(TypeType<T1,T2> tt1, TypeType<T1,T2> tt2) {
return tt1.t1.compareTo(tt2.t1);
}
}
}
这应该符合您的需要:
public static <U1 extends Comparable<? super U1>, U2 extends Comparable<? super U2>> Comparator<TypeType<U1, U2>> compareFirst() {
return new Comparator<TypeType<U1, U2>>() {
@Override
public int compare(TypeType<U1, U2> o1, TypeType<U1, U2> o2) {
return o1.t1.compareTo(o2.t1);
}
};
}
请注意,您将无法使用未使用不扩展 Comparable
.
的类型进行参数化的 TypeType
实例创建比较器
您可以使用以下 class 定义来强制 T1
和 T2
实现 Comparable
,因此您不必在运行时检查它:
public class TypeType<T1 extends Comparable<T1>, T2 extends Comparable<T2>> {
...
}
这里有几个很好的答案,但请注意,使用 Java 8 你可以 return 一个 lambda。
public static <T1 extends Comparable<? super T1>> Comparator<TypeType<T1,?>> comparatorFirst() {
return (tt1, tt2) -> tt1.t1.compareTo(tt2.t1);
}
public static <T2 extends Comparable<? super T2>> Comparator<TypeType<?, T2>> comparatorSecond() {
return (tt1, tt2) -> tt1.t2.compareTo(tt2.t2);
}
我想让下面的东西起作用:我有一个通用的 class public class TypeType<T1,T2>
,它有两个字段 public T1 t1
和 public T2 t2
。我想为此 class、compareFirst()
和 compareSecond()
编写两个 public static Comparator
方法。我希望第一种方法仅基于 t1
s 进行比较,而第二种方法仅基于 t2
s。第一种方法产生有效输出 if T1 implements Comparable<T1>
和第二种 if T2 implements Comparable<T2>
.
这样的事情可能吗?有没有办法检查 T1
和 T2
是否实现了 Comparable
?如果没有,我还能做这个工作吗? (此 class 仅供内部使用。)
非常感谢!
public class TypeType<T1,T2> {
public T1 t1;
public T2 t2;
public TypeType() {
this(null, null);
}
public TypeType(T1 t1, T2 t2) {
this.t1 = t1;
this.t2 = t2;
}
/*
* I know that the following does not work.
* This is just pseudo-code for what I would like to achieve.
*/
public static Comparator<TypeType<?,?>> comparatorFirst() {
return new Comparator<TypeType<?,?>> {
public int compare(TypeType<?,?> tt1, TypeType<?,?> tt2) {
return tt1.t1.compareTo(tt2.t1);
}
}
}
}
以防其他人感兴趣:我最终使用 Lambda 表达式实现了@pbabcdefp 的 Java 8 解决方案。
public static <T1 extends Comparable<? super T1>> Comparator<TypeType<T1,?>> compareOne() {
return (tt1, tt2) -> tt1.t1.compareTo(tt2.t1);
}
public static <T2 extends Comparable<? super T2>> Comparator<TypeType<?,T2>> compareTwo() {
return (tt1, tt2) -> tt1.t2.compareTo(tt2.t2);
}
public static <T1 extends Comparable<? super T1>,T2 extends Comparable<? super T2>> Comparator<TypeType<T1,T2>> compareOneThenTwo() {
return (tt1, tt2) -> {
if ( tt1.t1.equals(tt2.t1) )
return tt1.t2.compareTo(tt2.t2);
else
return tt1.t1.compareTo(tt2.t1);
};
}
public static <T1 extends Comparable<? super T1>,T2 extends Comparable<? super T2>> Comparator<TypeType<T1,T2>> compareTwoThenOne() {
return (tt1, tt2) -> {
if ( tt1.t2.equals(tt2.t2) )
return tt1.t1.compareTo(tt2.t1);
else
return tt1.t2.compareTo(tt2.t2);
};
}
instanceof
运算符可用于查看特定 class 是否派生自 class 或 class 是否实现特定接口。
即t1 instanceof Comparable
会 return true
如果它实现 Comparable
.
请注意泛型在运行时不存在,因此您只能检查是否实现了 Comparable
,而不是它实现了 Comparable<T1>
.
public static Comparator<TypeType<T1 extends Comparable<T1>,T1 extends Comparable<T2>>> comparatorFirst() {
return new Comparator<> {
public int compare(TypeType<T1,T2> tt1, TypeType<T1,T2> tt2) {
return tt1.t1.compareTo(tt2.t1);
}
}
}
这应该符合您的需要:
public static <U1 extends Comparable<? super U1>, U2 extends Comparable<? super U2>> Comparator<TypeType<U1, U2>> compareFirst() {
return new Comparator<TypeType<U1, U2>>() {
@Override
public int compare(TypeType<U1, U2> o1, TypeType<U1, U2> o2) {
return o1.t1.compareTo(o2.t1);
}
};
}
请注意,您将无法使用未使用不扩展 Comparable
.
TypeType
实例创建比较器
您可以使用以下 class 定义来强制 T1
和 T2
实现 Comparable
,因此您不必在运行时检查它:
public class TypeType<T1 extends Comparable<T1>, T2 extends Comparable<T2>> {
...
}
这里有几个很好的答案,但请注意,使用 Java 8 你可以 return 一个 lambda。
public static <T1 extends Comparable<? super T1>> Comparator<TypeType<T1,?>> comparatorFirst() {
return (tt1, tt2) -> tt1.t1.compareTo(tt2.t1);
}
public static <T2 extends Comparable<? super T2>> Comparator<TypeType<?, T2>> comparatorSecond() {
return (tt1, tt2) -> tt1.t2.compareTo(tt2.t2);
}