Java Error: Comparator is not abstract and does not override abstract method compare(Object,Object) in Comparator

Java Error: Comparator is not abstract and does not override abstract method compare(Object,Object) in Comparator

我有一个 Sorting.java 文件,其结构如下:

public class Sorting {
    public static <T> void mergeSort(T[] arr, Comparator<T> comparator) {
    (...)
    }
}

我的目标是在单独的 Driver.java 文件中测试 mergeSort() 方法。

为此,这是我在该驱动程序文件中包含的内容:

import java.util.Comparator;
import java.io.*;
import java.util.*;

public class Driver {
    public static void main(String[] args) {
        int[] testMergeSort1 = { 9, 13, 5, 6, 12, 10, 3, 7, 2, 8, 0, 15, 1, 4, 14, 11 };
        mergeSort(testMergeSort1, IntegerComparator);
        System.out.println(Arrays.toString(testMergeSort1));
    }
}

并且,根据 this similar question 的已接受答案,我将整数比较器的以下实现添加到同一个 Driver.java 文件:

public class IntegerComparator implements Comparator {
    public int compare(Integer a, Integer b) {
        return a.intValue() - b.intValue();
    }

    public int equals(Object obj) {
        return this.equals(obj);
    }
}

但是,当我编译 Driver.java 时,出现以下错误:

Driver.java:33: error: Driver.IntegerComparator is not abstract and does not override abstract method compare(Object,Object) in Comparator
    public class IntegerComparator implements Comparator {
           ^
Driver.java:38: error: equals(Object) in Driver.IntegerComparator cannot implement equals(Object) in Comparator
        public int equals(Object obj) {
                   ^
  return type int is not compatible with boolean

任何关于如何从单独的 Driver.java 文件中的 Sorting.java 文件继续测试 mergeSort() 方法的指导,我们将不胜感激。

尝试实现一个通用的 Comparator<Integer> 并添加 @Override 注释,以检查您是否真的覆盖了 Comparator 中的方法。

public class IntegerComparator implements Comparator<Integer> {
    @Override
    public int compare(Integer a, Integer b) {
        return a.intValue() - b.intValue();
    }
}