Java 方法 compare(long, long) 对于 Long 类型是未定义的
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20163122/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
The method compare(long, long) is undefined for the type Long
提问by Vlad Alexeev
I need to make a Comparator to sort my List of objects by one of its variables which is of long
type.
我需要制作一个比较器来按其long
类型的变量之一对我的对象列表进行排序。
public class ParticipantIndexComparator implements Comparator<Integer> {
final List<Participant> participants;
public ParticipantIndexComparator(ArrayList<Integer> numbersToSort) {
participants=new ArrayList<Participant>();
for (int i=0;i<numbersToSort.size();i++)
{ participants.add(i,competition.participant.get(numbersToSort.get(i))); participants.get(i).comparator=numbersToSort.get(i);}
}
@Override
public int compare(Integer i1, Integer i2) {
long l1 = participants.get(i1).kpTime.get(kpSelected);
long l2 = participants.get(i2).kpTime.get(kpSelected);
return Long.compare(l1, l2);
}
}
But return Long.compare(l1, l2);
is invalid - "The method compare(long, long) is undefined for the type Long".
但 return Long.compare(l1, l2);
无效 - “未定义 Long 类型的方法 compare(long, long)”。
Seems like I'm doing it the wrong way.
好像我做错了。
采纳答案by JB Nizet
This method exists since Java 7. You're probably using a Java 6 or even older version of Java. In these previous versions, you can simply use
此方法自 Java 7 以来就存在。您可能使用的是 Java 6 或更旧版本的 Java。在这些以前的版本中,您可以简单地使用
Long.valueOf(l1).compareTo(Long.valueOf(l2));
The javadocis your friend. Read it.
该javadoc的是你的朋友。阅读。
回答by eric gilbertson
If you experience Integer.compare being undefined after importing a Maven project into Eclipse, check the project properties to insure that it is using a 1.7 JRE. For some reason the import associated a 1.6 JRE with my project even though the POM, and my system default was 1.7.*.
如果您在将 Maven 项目导入 Eclipse 后遇到 Integer.compare 未定义,请检查项目属性以确保它使用的是 1.7 JRE。出于某种原因,导入将 1.6 JRE 与我的项目相关联,即使是 POM,我的系统默认值为 1.7.*。