Using Java 7 Comparators in Java 8
java, java-8, migration, osgi
Solution
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.
Problem
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?