It is known that autoboxing can be computationally intensive, but it seems to me that allowing primitive arrays be autoboxed to their Object
equivalent for sorting methods is an appropriate edge case, particularly when the values are being sorted based on some external calculation.
I had an issue earlier, where a given array of primitive int
s needed to be sorted by the results of a calculation based on the index value. However,java.util.Comparator
does not allow primitive types for any implementing classes and compare(T x, T y)
methods.
So instead of doing something like
public void sortRow(int row){
Arrays.sort(this.buffer[row], new Comparator<Integer>(){
@Override
public int compare(int x, int y){
return (PhotoUtils.getBrightnessValue(x) <= PhotoUtils.getBrightnessValue(y) ? x : y;
}
}
}
I had to implement a second step:
public void sortRow(int row){
Integer[] tempArray = new Integer[this.buffer[row].length];
for (int i = 0; i < tempArray.length; i++)
tempArray[i] = this.buffer[row][i];
Arrays.sort(tempArray, new Comparator<Integer>(){
@Override
public int compare(Integer x, Integer y){
return (PhotoUtils.getBrightnessValue(x) <= PhotoUtils.getBrightnessValue(y) ? x : y;
}
}
}
According to the JLS, Section 5.1.7, there are only the following specified boxing conversions, and arrays aren't involved in any of them: