Java 覆盖 compareTo、Long

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19301547/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 15:50:28  来源:igfitidea点击:

Java override compareTo, Long

javacomparablecompareto

提问by user1121487

I have a class that implements the Comparable interface. In this class I need to override compareTo method in order to sort objects by Longvalues.

我有一个实现 Comparable 接口的类。在这个类中,我需要覆盖 compareTo 方法以便按Long值对对象进行排序。

What I don't know is how to perform is the comparison of the Long type. I get error when trying to check if value is greater than or less than another Long value. I know Long is the object of long, but have no idea how to compare two Long's.

我不知道的是如何执行的是 Long 类型的比较。尝试检查值是否大于或小于另一个 Long 值时出现错误。我知道Long是long的对象,但不知道如何比较两个Long的。

Code sample:

代码示例:

public int compareTo(MyEntry<K, V> object) {
    if (this.value < object.value)
        return -1;
    if (this.value.equals(object.value))
        return 0;

    return 1;
}

Error message:

错误信息:

           operator < cannot be applied to V,V
if (this.value < object.value)
                       ^

V, V is Long, Long

V, V 长, 长

采纳答案by StormeHawke

Your problem is that MyEntry<K, V>doesn't tell the compiler what type of Object you're trying to compare. It doesn't know that you're comparing Long values. The best way to do this is to not worry about what type of Object you're comparing (assuming your object implements Comparable) by just using

您的问题是MyEntry<K, V>没有告诉编译器您要比较的对象类型。它不知道您正在比较 Long 值。做到这一点的最佳方法是不用担心您正在比较的对象类型(假设您的对象实现了 Comparable),只需使用

return this.value.compareTo(object.value);

but if you want to do it manually for some reason, do this:

但如果您出于某种原因想手动执行此操作,请执行以下操作:

public int compareTo(MyEntry<K, V> object) {
    if ((Long) this.value < (Long) object.value)
        return -1;
    if (this.value.equals(object.value))
        return 0;

    return 1;
}

回答by Scientist

Use longValue() method for comparison of long values.

使用 longValue() 方法比较长值。

eg:-

例如:-

Long id1 = obj.getId();
Long id2 = obj1.getId();

if (id1.longValue() <= id2.longValue()) {
Sysout.......
}

assertTrue(id1.longValue() == id2.longValue())

回答by Adel Boutros

Long l1 = new Long(3);
Long l2 = new Long(2);

return l1.compareTo(l2);

Simple no?

简单不?

回答by Sir Snowman

The long compareTo command might help. The compareTo method returns an integer value to give you an answer as to whether the longs are equal, greater than, or less than each other.

长的 compareTo 命令可能会有所帮助。compareTo 方法返回一个整数值,以给出有关 long 是否相等、大于或小于彼此的答案。

Long l1 = new Long(63255);
 Long l2 = new Long(71678);
 int returnVal =  l1.compareTo(l2);

 if(returnVal > 0) {
    System.out.println("l1 is greater than l2");
 }
 else if(returnVal < 0) {
    System.out.println("l1 is less than l2");
 }
 else {
    System.out.println("l1 is equal to l2");
 }

回答by Vidya

It would look something like this:

它看起来像这样:

@Override
public int compareTo(MyEntry<K, V> object) {
        if (object == null) {
            throw new NullPointerException("Null parameter");
        } else if (!this.getClass().equals(object.getClass())) {
            throw new ClassCastException("Possible ClassLoader issue.");
        } else {
            return this.longValue.compareTo(object.longValue);
        }

}

Coincidentally, we recently did a tutorialon comparisons in Java. Maybe it can help you.

巧合的是,我们最近做了一个关于 Java 比较的教程。也许它可以帮助你。

回答by Kai Wang

Cast long to Long, then use compareTo method of Long.

将 long 转换为 Long,然后使用 Long 的 compareTo 方法。

Java is well structured, nearly all sortable Class has compareTo method.

Java 结构良好,几乎所有可排序的类都有 compareTo 方法。

This is a good Java practice.

这是一个很好的 Java 实践。

@Override
                public int compare(long t1, long t2) {
                    return Long.valueOf(t1).compareTo(t2);
                }