I am referencing my java version JDK 1.8 but I am still getting error. What is wrong with this referencing (writing Java after 6 years)? or any other simpler way to do this? I d
Your declaration of MyComparator
declares a generic type of name Long
, and that then shadows java.lang.Long
. Your class shouldn't be generic. Also, you don't need Long.valueOf
since you already have Long
instance. Change it to remove the generic, like
public class MyComparator implements Comparator<Long> {
@Override
public int compare(Long long1, Long long2) {
return long1.compareTo(long2);
}
}
Alternatively anytime you need Comparator<Long>
or any other class
that implements Comparable
you can call:
Comparator.naturalOrder()
https://docs.oracle.com/javase/8/docs/api/java/util/Comparator.html#naturalOrder--