简体   繁体   English

使用比较器时同一类型出现不兼容类型错误

[英]Incompatible Type error on same type when using Comparator

I have a generic class called Interval我有一个名为Interval的通用 class

package com.test;

//import statements

public class Interval<T extends Comparable<? super T>> {

    private boolean isStartInclusive;
    private T start;
    private T end;
    private boolean isEndInclusive;

    //Constructors

    //Getter and Setters

    //ToString

    //Hashcode and Equals

    //A public static method that returns comparator for use by other classes
    public static <T extends Comparable<? super T>> Comparator<Interval<T>> getIntervalComparator() {
        return (o1, o2) -> {
            //comparing logic that returns -1 or 1 or 0
        };
    }

}

Whenever I use this Comparator to sort a List<Interval<T>> it works.每当我使用此ComparatorList<Interval<T>>进行排序时,它都会起作用。

List<Interval<T>> intervalsList1 = getIntervalsList();
intervalsList1.sort(Interval.getIntervalComparator());

But when I do this但是当我这样做时

List<Interval<T>> intervalsList1 = getIntervalsList1();
List<Interval<T>> intervalsList2 = getIntervalsList2();

Interval<T> interval1 = intervalsList1.get(i);
Interval<T> interval2 = intervalsList2.get(i);

int compareOneVsTwo = Interval.getIntervalComparator().compare(interval1, interval2); //line 85

I get an error saying我收到一条错误消息

Error:(85, 76) java: incompatible types: com.test.Interval<T> cannot be converted to com.test.Interval<T>

Even when I do this即使我这样做

Interval<Integer> integerInterval1 = new Interval<>(true, 1, 2, true);
Interval<Integer> integerInterval2 = new Interval<>(true, 4, 5, true);
int compareOneVsTwo = Interval.getIntervalComparator().compare(integerInterval1, integerInterval2); //line 115

I get this error:我收到此错误:

Error:(115, 72) java: incompatible types: com.test.Interval<java.lang.Integer> cannot be converted to com.test.Interval<T>

I do not understand what is causing this compilation error.我不明白是什么导致了这个编译错误。 The comparator inside the Interval class is not the natural sort order, but the order in which multiple classes use when they sort for their own purpose. Interval class里面的comparator不是自然的排序顺序,而是多个类为了自己的目的排序时使用的顺序。

It should work if you separate the statement into two separate statements:如果将语句分成两个单独的语句,它应该可以工作:

Comparator<Interval<T>> compareOneVsTwo = Interval.getIntervalComparator();
compareOneVsTwo.compare(interval1, interval2);

// or in the integer case:
Comparator<Interval<Integer>> compareOneVsTwo = Interval.getIntervalComparator();
compareOneVsTwo.compare(interval1, interval2);

(assuming T is an existing type) (假设T是现有类型)

This is because when they are the same statement, the compiler is not smart enough to know that you need a Comparator<Interval<Integer>> .这是因为当它们是相同的语句时,编译器不够聪明,无法知道您需要Comparator<Interval<Integer>> You could need a Comparator<Interval<String>> or a Comparator<Interval<LocalDate>> , as far as the compile is concerned, because it doesn't look at the .compare call when inferring the type parameters for the getIntervalComparator call.就编译而言,您可能需要Comparator<Interval<String>>Comparator<Interval<LocalDate>>因为它在推断getIntervalComparator调用的类型参数时不查看.compare调用。

What happens when the compiler doesn't know?当编译器不知道时会发生什么? It uses the type bound on the type parameter, ie Comparable<? super T>它使用绑定在类型参数上的类型,即Comparable<? super T> Comparable<? super T> , so it thinks you need a Comparator<Interval<T>> , but T doesn't actually exist... Comparable<? super T> ,所以它认为你需要一个Comparator<Interval<T>> ,但T实际上并不存在......

Another way to fix this is:解决此问题的另一种方法是:

Interval.<Integer>getIntervalComparator().compare(...)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM