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 did some search and these functions are available in later java versions. Eclipse is Oxygen
The method valueOf(Long) is undefined for the type Long
The method compareTo() is undefined for the type Long
import java.util.Comparator;
import java.lang.Long;
public class MyComparator implements Comparator{
@Override
public int compare(Long long1, Long long2) {
//Long.compareTo()
return Long.valueOf(long1).compareTo(Long.valueOf(long2));
}
}
and JDK points to
/Library/Java/JavaVirtualMachines/jdk1.8.0_25.jdk/Contents/Home
解决方案
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 {
@Override
public int compare(Long long1, Long long2) {
return long1.compareTo(long2);
}
}