Reputation: 11262
Is there a reason why java.util.Arrays class does not support a compareTo function for different array types, specifically byte[] arrays? I have found a sample implementation here, but I'm mostly wondering if I'm overlooking some common Java class.
EDIT: compareTo does not behave like Arrays.equals but more so like String.compareTo
Upvotes: 1
Views: 4776
Reputation: 2237
Following is a generic compare()
function for arrays in Java. You can modify for your needs in case you have a different interpretation for what makes one array "greater than" another in non-straightforward scenarios (different lengths, nulls, etc.). It can also be changed easily to work on primitives, such as byte
, int
, etc.
public static <T extends Comparable<T>> int compareArray(T[] a, T[] b) {
if (a == b) { // also covers the case of two null arrays. those are considered 'equal'
return 0;
}
// arbitrary: non-null array is considered 'greater than' null array
if (a == null) {
return -1; // "a < b"
} else if (b == null) {
return 1; // "a > b"
}
// now the item-by-item comparison - the loop runs as long as items in both arrays are equal
int last = Math.min(a.length, b.length);
for (int i = 0; i < last; i++) {
T ai = a[i];
T bi = b[i];
if (ai == null && bi == null) {
continue; // two null items are assumed 'equal'
} else if (ai == null) { // arbitrary: non-null item is considered 'greater than' null item
return -1; // "a < b"
} else if (bi == null) {
return 1; // "a > b"
}
int comp = ai.compareTo(bi);
if (comp != 0) {
return comp;
}
}
// shorter array whose items are all equal to the first items of a longer array is considered 'less than'
if (a.length < b.length) {
return -1; // "a < b"
} else if (a.length > b.length) {
return 1; // "a > b"
}
// i.e. (a.length == b.length)
return 0; // "a = b", same length, all items equal
}
Upvotes: 3
Reputation: 109532
No specific reason, other than code bloating, which always has played a major role in java: the Java installation always been called a large overhead. A compareTo on byte[]
would be signed wouldn't it? Would you like an unsigned too? How often would it be used? Maybe nothing for the core Java.
But you have a point: equals could have been implemented using compareTo.
Upvotes: 2
Reputation: 51323
The meaning of compareTo on an array depends on your use case. Thus it is not implemented in java.util.Arrays and because not every array contains comparable objects..
If you want to implement a compareTo
Method on an array you have to answer the question:
When is one array less than, equal to or greater than another array?
If the array is a byte[]
that is based on an ascii string it might be easy, but then you have a special use case.
Take a look at these questions to understand what I mean:
Comparable
s when you don't know much more about the elements than that they are Comparable
s? Comparable
s is less than another array of Comparable
s because each element at the same index is less than the other array's element at the same index and what would you do with such information?I think that the byte[]
is a very specific use case, becuse you think of the byte[]
as a string of ascii characters, but remember that the byte[]
representation of a string depends of the encoding.
Upvotes: 1