Is there a library to compare primitive type values?
Asked Answered
D

4

5

I am implementing Comparable interface on a trivial class that wraps a single int member.

I can implement it this way:

    @Override
    public int compareTo ( final MyType o )
    {
        return
            Integer.valueOf( this.intVal ).compareTo(
                Integer.valueOf( o.intVal )
            );
    }

But this (maybe) creates 2 totally unnecessary Integer objects.

Or I can go tried and true cut-and-paste approach from 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));
    }

This is pretty efficient, but duplicates code unnecessary.

Is there a library that would implement this missing Integer ( and Double and Float ) method?

   public static int compare ( int v1, int v2 );
Diminish answered 1/9, 2011 at 16:4 Comment(1)
Could you copy the Integer compareTo() to a new method that takes two arguments instead of using this?Studley
D
6

In Java 7, static int compare for primitive types have been added to all primitive object wrapper classes, i.e there is now:

java.lang.Integer: static int compare( int x, int y );
java.lang.Byte: static int compare( byte x, byte y );
java.lang.Short: static int compare( short x, short y );
etc...
Diminish answered 31/7, 2013 at 11:7 Comment(0)
C
6
  • For int, write your own compare method (it requires at most three lines of code).
  • For double, use Double.compare (not to be confused with compareTo).
  • For float, use Float.compare.

The last two take primitive types and thus avoid boxing and unboxing. I can see an argument for Integer providing a similar compare method, but as things stand it doesn't.

Complexioned answered 1/9, 2011 at 16:14 Comment(5)
Integer also has a compareSelma
@Steve Kuo: Link to Javadoc please?Complexioned
You are correct. I was thinking of compareTo, in which case he could do Integer.valueOf(i1).compareTo(i2).Selma
@Steve Kuo: No, it doesn't, that's the point. And, by the way, if you've read the question I know how to do Integer.valueOfDiminish
+1: For byte, short, int and float you can use Double.compare().Kindness
D
6

In Java 7, static int compare for primitive types have been added to all primitive object wrapper classes, i.e there is now:

java.lang.Integer: static int compare( int x, int y );
java.lang.Byte: static int compare( byte x, byte y );
java.lang.Short: static int compare( short x, short y );
etc...
Diminish answered 31/7, 2013 at 11:7 Comment(0)
U
2

Maybe, I'm missing something, but IMHO this is a weird question.

Is there a library that would implement this missing Integer ( and Double and Float ) method?

public static int compare ( int v1, int v2 );

Well, I think this does the job:

public static int compare ( int v1, int v2 )
{
    if (v1 < v2) return -1;
    if (v1 > v2) return  1;
    return 0;
}
Untenable answered 1/9, 2011 at 16:10 Comment(1)
@barjak: Nope, please check #7273591 and it comments.Untenable
R
-1

This solution is simple, but perhaps simplistic:

public static int compare ( int v1, int v2 )
{
    return v1 - v2;
}

Note: @aix is correct! This approach will not work for arbitrary integers. It will work for always-positive integers though, for example auto generated database keys etc

Restless answered 1/9, 2011 at 16:16 Comment(4)
How about integer overflow?Complexioned
@Martijn Courteaux, you are incorrect, compare and compareTo specify negative, 0, positive values. It doesn't dictate -1, 0 1. Any code that checks for -1 is bad and broken code. See download.oracle.com/javase/6/docs/api/java/lang/…Selma
No, the Javadoc says "negative, zero or positive", for both Comparable and Integer.Overmatter
@Steve Kuo: Oops, indeed! Thanks, I learnt something today :DUntenable

© 2022 - 2024 — McMap. All rights reserved.