Java Long Compare and ValueOf method undefined

后端 未结 2 757
我寻月下人不归
我寻月下人不归 2021-01-26 20:01

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

相关标签:
2条回答
  • 2021-01-26 20:08

    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);
        }
    }
    
    0 讨论(0)
  • 2021-01-26 20:25

    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--

    0 讨论(0)
提交回复
热议问题