I implement an interface Comparablein a trivial class that wraps one member int.
I can implement it like this:
@Override
public int compareTo ( final MyType o )
{
return
Integer.valueOf( this.intVal ).compareTo(
Integer.valueOf( o.intVal )
);
}
But this (possibly) creates 2 completely unnecessary Integer objects.
Or I can use a proven and truthful approach in the Integer class:
@Override
public int compareTo ( final MyType o )
{
int thisVal = this.intValue;
int anotherVal = o.intValue;
return (thisVal<anotherVal ? -1 : (thisVal==anotherVal ? 0 : 1));
}
It is quite efficient, but duplicates the code unnecessary.
Is there a library that implements this missing method Integer(both Double and Float)?
public static int compare ( int v1, int v2 );
source
share