Re: Did the sort do anything?
On 5/20/2011 2:56 PM, Lawrence D'Oliveiro wrote:
In message<e5CdnR_-upYpwUvQnZ2dnUVZ_uSdnZ2d@earthlink.com>, Patricia Shanahan wrote:
On 5/20/2011 5:00 AM, Patricia Shanahan wrote:
....
To further refresh your memory, the question I raised was whether you
consider System.identityHashCode(x) to be part of the "entire object
state" of the object referenced by x.
What is the significance of sorting on a hash function?
Sorting affects the order of events. Suppose there are a lot of equal
objects. At hash map size N, many of them fall in the same bucket. At a
higher size, they gets spread over two or more buckets, reducing
collisions. The stability, or instability, of a sort algorithm can
change the order of appearance, affecting which keys are added before
resizing.
That is just one way in which changing the order in a list of equal
objects may be visible. If that is too subtle for your taste, consider
the following:
for(Integer x : myArray){
System.out.println(x + " " + System.identityHashCode(x));
}
Arrays.sort(myArray);
for(Integer x : myArray){
System.out.println(x + " " + System.identityHashCode(x));
}
If the sort is stable, Integer objects with the same value appear in the
same order in the two printouts. If it is not stable, they may be reordered.
Patricia