[英]Combining varargs and generics for chained comparisons in Java
Here's a tough nut to crack. 这是一个难以克服的难题。 I have a clash between using varargs and generics together.
我在一起使用varargs和泛型之间有冲突。 Following given code:
以下给出的代码:
public class MyObject implements Comparable<MyObject>
{
private String name;
private int index;
@Override
public int compareTo(MyObject o)
{
if (name.compareTo(o.name) != 0)
return name.compareTo(o.name);
return ((Integer) index).compareTo(o.index);
}
}
I want the compareTo
method to use more than one compare condition. 我希望
compareTo
方法使用多个比较条件。 If the strings are the same then use the ints instead. 如果字符串相同,则使用整数。 Usual situation I would say.
我会说通常的情况。
I would love to create a static method to handle this in general. 我很想创建一个静态方法来处理这个问题。 And I want the new method
chainedCompare
to be called like this: 我希望这样调用新方法
chainedCompare
:
public int compareTo(MyObject o)
{
return chainedCompare(this, o, myO -> myO.name, myO -> myO.index);
}
The lambdas are varargs of the Java 8 interface Function. lambda是Java 8接口Function的varargs。 So first I wrote the method like that:
所以首先我写了这样的方法:
public static <T, C extends Comparable<C>> int chainedCompare(T object1, T object2, Function<T, C>... comparisons)
{
int compareValue = 0;
for (Function<T, C> comparison : comparisons)
{
compareValue = comparison.apply(object1).compareTo(comparison.apply(object2));
if (compareValue != 0)
break;
}
return compareValue;
}
But I didn't consider that in this case the generic type C must be the same type for all Function<T, C>
comparisons in the varargs array. 但是我不认为在这种情况下,泛型C对于varargs数组中的所有
Function<T, C>
比较必须是相同的类型。 As you can see above, I want to use different Comparables (like String and Integer in the example). 从上面可以看到,我想使用不同的Comparables(例如示例中的String和Integer)。
Then I modified it to this version: 然后我将其修改为以下版本:
public static <T> int chainedCompare(T object1, T object2, Function<T, ? extends Comparable<?>>... comparisons)
{
int compareValue = 0;
for (Function<T, ? extends Comparable<?>> comparison : comparisons)
{
compareValue = comparison.apply(object1).compareTo(comparison.apply(object2));
if (compareValue != 0)
break;
}
return compareValue;
}
Type C is here replaced with wildcards. C型在这里被替换为通配符。 While the method call would work now, the method itself does not compile, because of the wildcard typed parameter of
compareTo
. 尽管该方法调用现在可以正常工作,但是由于
compareTo
的通配符类型参数,该方法本身无法编译。
So on the one hand I need a fixed generic type (extends Comparable) for the Function interface, but on the other hand I need Function interfaces of different (second) generic types where you usually could set a wildcard. 因此,一方面我需要一个固定的泛型类型(扩展Comparable)作为Function接口,但是另一方面,我需要具有不同(第二种)泛型类型的Function接口,通常可以设置通配符。 How to resolve this?
如何解决呢?
My only requirement is that I can call the static method as simple as shown with an undefined number of comparison conditions. 我唯一的要求是,我可以像显示的未定义数量的比较条件一样简单地调用静态方法。
Based on the suggestions of Tunaki I was able to modify the method as follows which can be used like desired: 根据Tunaki的建议,我可以对方法进行如下修改,可以按需使用:
@SuppressWarnings("raw-types")
public static <T> int chainedCompare(T object1, T object2, Function<T, ? extends Comparable>... comparisons)
{
return Arrays.stream(comparisons)
.map(Comparator::comparing)
.reduce(Comparator::thenComparing)
.map(c -> c.compare(object1, object2))
.orElse(0);
}
public int compareTo(MyObject o)
{
return chainedCompare(this, o, myO -> myO.name, myO -> myO.index);
}
Instead of using a Comparable
, it would be easier to use a Comparator
: 与其使用
Comparable
, Comparable
使用Comparator
:
public static <T> int chainedCompare(T object1, T object2, Comparator<T>... comparators) {
int compareValue = 0;
for (Comparator<? super T> comparator : comparators) {
compareValue = comparator.compare(object1, object2);
if (compareValue != 0)
break;
}
return compareValue;
}
You could also chain all the comparator together using thenComparing
and have 您还可以使用
thenComparing
所有比较器链接在一起,并具有
@SafeVarargs
public static <T> int chainedCompare(T object1, T object2, Comparator<T>... comparators) {
return Arrays.stream(comparators)
.reduce(Comparator::thenComparing)
.map(c -> c.compare(object1, object2))
.orElse(0);
}
Then you can use that by constructing Comparator
objects with comparing(keyExtractor)
or the primitive specialization comparingInt
. 然后你就可以使用通过构建
Comparator
与对象comparing(keyExtractor)
或原始专业化comparingInt
。
@Override
public int compareTo(MyObject o) {
return chainedCompare(this, o,
Comparator.comparing(obj -> obj.name),
Comparator.comparingInt(obj -> obj.index)
);
}
With this approach, you can even question the existence of such utility and simply have 使用这种方法,您甚至可以质疑这种实用程序的存在,并且只需
@Override
public int compareTo(MyObject o) {
return Comparator.<MyObject, String> comparing(obj -> obj.name)
.thenComparingInt(obj -> obj.index)
.compare(this, o);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.