Using Java 7 Comparators in Java 8

thobens picture thobens · Mar 22, 2014 · Viewed 7.1k times · Source

Situation

I have an OSGi project that I'm trying to migrate to Java 8. In my project, I have dependencies to third party libraries that I "OSGi-fied" (by just adding the MANIFEST.MF file and putting metadata into it). These libraries are checked out from read-only SVN repositories, so I just can checkout updates from then when needed and therefore I don't want to make any other changes than in the MANIFEST.MF file, since I cannot commit them.

Problem

However, these libraries use lots of anonymous Comparators like:

private static final Comparator heightComparator = new Comparator() {
    public int compare (Object o1, Object o2) {
        return ((Glyph)o1).getHeight() - ((Glyph)o2).getHeight();
    }
};

Now, apparently the java.util.Comparator interface has a whole bunch of new methods that need to be implemented (which, of course, leads to compilation errors). But i really want to avoid implementing them or switch to Lambda expressions because modifying the original source would most likely result in conflicts each time I check out newer revisions.

Java used to work hard on backwards compatibility and I wonder why such a simple and widely used part of the API needs so (relatively) much effort to migrate. Am I missing something or is it really unavoidable?

Answer

jandy picture jandy · May 1, 2014

I had a similar problem like thobens: Updated my Kepler 4.2.3 to use Java 8, set JAVA 8 as new JRE, etc. Like thobens I got an error on Comparators by eclipse requesting me to implement all unimplemented methods.

Finally this was caused by the old Compiler compilance level (1.7) - switching to 1.8 solved this trouble.