Question

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?

Was it helpful?

Solution 2

The new methods in the Java 8 version of Comparator are default methods (a new feature in Java 8) which not only include the method signature, but also a default implementation. Thus, older Comparator implementations should work just fine on Java 8 if they worked before.

If something is not working, please let us know what you are trying and what the error message is.

OTHER TIPS

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.

that happen because for java 7 Comparator does not had all the other methods, when you update Eclipse to Java 1.8 then the new methods are needed in order to use Comparator, to solve this just in change the Compiler back to Java 1.7 ( properties - Java Compiler --- JDK Compliance ), refresh your project an that it.. hope this help

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top