Comparator<String> must override super class method

Kenny Wyland picture Kenny Wyland · Aug 28, 2011 · Viewed 11.4k times · Source

I'm making a TreeMap<String, String> and want to order it in a descending fashion. I created the following comparator:

Comparator<String> descender = new Comparator<String>() {

    @Override
    public int compare(String o1, String o2) {
        return o2.compareTo(o1);
    }
};

I construct the TreeMap like so:

myMap = new TreeMap<String, String>(descender);

However, I'm getting the following error:

The method compare(String, String) of type new Comparator<String>(){} must override a superclass method

I've never fully groked generics, what am I doing wrong?

Answer

BalusC picture BalusC · Aug 28, 2011

Your Eclipse project is apparently set to Java 1.5. The @Override annotation is then indeed not supported on interface methods. Either remove that annotation or fix your project's compliance level to Java 1.6.