Java override compareTo, Long
Asked Answered
C

6

5

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

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.

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

Chanell answered 10/10, 2013 at 16:42 Comment(4)
Please show your code attempt and your full error message.Guard
There is compareTo method in LongZeeba
@SubirKumarSao With a little more detail, that should be an answerIrrecoverable
@Chanell Your implementation of compareTo seems to be wrong. Post the Class for which you are implementing compareTo.Zeeba
A
9

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

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;
}
Alita answered 10/10, 2013 at 16:52 Comment(4)
This is what I'm looking for. I thought the type would be there since it's added when the the class was instanced. This works. Thanks allot!Chanell
I would add that when you declare your class as implements Comparator you can explicitly specify implements Comparable<KeyClass, Long> so you don't have to worry about all the castingAlita
if value is a Long, why wouldn't you just delegate it to Long's compareTo (see Adel's answer)?Qualm
@SteveKuo that's what I did in my first code block... there's two up there, did you read the whole answer?Alita
O
8
Long l1 = new Long(3);
Long l2 = new Long(2);

return l1.compareTo(l2);

Simple no?

Oakes answered 10/10, 2013 at 16:48 Comment(0)
F
2

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 tutorial on comparisons in Java. Maybe it can help you.

Flense answered 10/10, 2013 at 16:55 Comment(0)
M
2

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

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

This is a good Java practice.

@Override
                public int compare(long t1, long t2) {
                    return Long.valueOf(t1).compareTo(t2);
                }
Mestee answered 13/6, 2017 at 19:57 Comment(1)
Or Long.compare(t1, t2) // since Java 1.7Artemis
S
0

Use longValue() method for comparison of long values.

eg:-

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

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

assertTrue(id1.longValue() == id2.longValue())
Sordid answered 10/10, 2013 at 16:48 Comment(1)
Autoboxing starting in Java 1.5 makes this irrelevantAlita
R
-1

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.

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");
 }
Ruthenic answered 10/10, 2013 at 16:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.