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?
Comparator has undergone a major overhaul in Java 8 while still retaining its essence which is to compare and sort objects in Collections. Comparator now supports declarations via lambda expressionsRead Lambda Expressions Tutorial as it is a Functional Interface.
Java String compareTo() Method The compareTo() method compares two strings lexicographically. The comparison is based on the Unicode value of each character in the strings. The method returns 0 if the string is equal to the other string.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With