Java Long Compare and ValueOf method undefined

倖福魔咒の 提交于 2019-12-02 04:07:12

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!