compareTo with primitives -> Integer / int
Asked Answered
V

8

71

Is it better to write

int primitive1 = 3, primitive2 = 4;
Integer a = new Integer(primitive1);
Integer b = new Integer(primitive2);
int compare = a.compareTo(b);

or

int primitive1 = 3, primitive2 = 4;
int compare = (primitive1 > primitive2) ? 1 : 0;
if(compare == 0){
    compare = (primitive1 == primitive2) ? 0 : -1;
}

I think the second one is better, should be faster and more memory optimized. But aren't they equal?

Veneration answered 5/2, 2012 at 15:30 Comment(4)
never call "new Integer" instead using Integer.valueOf(int) (docs.oracle.com/javase/6/docs/api/java/lang/…) this method accesses an internal cache and will often avoid an allocation for small integers.Bicyclic
FYI: On modern JVMs (like HotSpot), the two programs will probably become the same machine code after optimization. The best way to know for sure is (as always) benchmark both solutions.Shipboard
In addition to @Bicyclic 's comment: new Integer() will be deprecated in Java9 download.java.net/java/jdk9/docs/api/java/lang/…Betti
docs.oracle.com/javase/9/docs/api/java/lang/… is a working linkPaleo
U
147

For performance, it usually best to make the code as simple and clear as possible and this will often perform well (as the JIT will optimise this code best). In your case, the simplest examples are also likely to be the fastest.


I would do either

int cmp = a > b ? +1 : a < b ? -1 : 0;

or a longer version

int cmp;
if (a > b)
   cmp = +1;
else if (a < b)
   cmp = -1;
else
   cmp = 0;

or

int cmp = Integer.compare(a, b); // in Java 7
int cmp = Double.compare(a, b); // before Java 7

It's best not to create an object if you don't need to.

Performance wise, the first is best.

If you know for sure that you won't get an overflow you can use

int cmp = a - b; // if you know there wont be an overflow.

you won't get faster than this.

Unkennel answered 5/2, 2012 at 15:33 Comment(3)
Integer doesn't have an equivalent static method before Java 7. :( But Double.compare() has the same effect.Unkennel
Never use cmp = a - b, even if you "know" there won't be an overflow. You don't know there won't be an overflow.Flirtation
There won't be an overflow if both a and b are positive integers, for example.Compulsive
T
66

Use Integer.compare(int, int). And don'try to micro-optimize your code unless you can prove that you have a performance issue.

Teen answered 5/2, 2012 at 15:34 Comment(4)
this method is in JDK since 1.7, I'm sorry, but for compatibility issues I can't use it so far.Veneration
Any pre-1.7 example like this?Hauge
@Gaʀʀʏ, see Peter's answer above.Teen
This is the only correct answer. Additionally, the entire implementation of Integer.compare(x, y) is return (x < y) ? -1 : ((x == y) ? 0 : 1); so on JDK < 1.7 you can paste that into your own static utility methodFlirtation
M
15

May I propose a third

((Integer) a).compareTo(b)  
Milda answered 5/2, 2012 at 15:33 Comment(0)
A
5

Wrapping int primitive into Integer object will cost you some memory, but the difference will be only significant in very rare(memory demand) cases (array with 1000+ elements). I will not recommend using new Integer(int a) constructor this way. This will suffice :

Integer a = 3; 

About comparision there is Math.signum(double d).

compare= (int) Math.signum(a-b); 
Atkins answered 5/2, 2012 at 15:44 Comment(1)
This will overflow, since the int to double conversion happens after the subtraction.Flirtation
B
4

They're already ints. Why not just use subtraction?

compare = a - b;

Note that Integer.compareTo() doesn't necessarily return only -1, 0 or 1 either.

Brigandage answered 27/5, 2015 at 22:57 Comment(1)
This will produce erroneous results in the case of overflow. Try a = Integer.MAX_VALUE and b = -3. The result will be negative, indicating that a < b.Sibilant
I
4

For pre 1.7 i would say an equivalent to Integer.compare(x, y) is:

Integer.valueOf(x).compareTo(y);
Indies answered 17/11, 2015 at 15:59 Comment(0)
P
3

If you are using java 8, you can create Comparator by this method:

Comparator.comparingInt(i -> i);

if you would like to compare with reversed order:

Comparator.comparingInt(i -> -i);
Paulitapaulk answered 20/5, 2017 at 6:53 Comment(0)
B
-1

If you need just logical value (as it almost always is), the following one-liner will help you:

boolean ifIntsEqual = !((Math.max(a,b) - Math.min(a, b)) > 0);

And it works even in Java 1.5+, maybe even in 1.1 (i don't have one). Please tell us, if you can test it in 1.5-.

This one will do too:

boolean ifIntsEqual = !((Math.abs(a-b)) > 0);
Bulldozer answered 15/12, 2017 at 16:35 Comment(1)
These expressions perform a lot of wasted computation to (in the first case, incorrectly) compute a == b, which is not what the question is about.Flirtation

© 2022 - 2024 — McMap. All rights reserved.