Given a list of numbers, for example some unique integer or long ID's what would be an optimum way to compute a reproducible 'signature' (preferably irregardless of element order)?
The use case is to detect whether any of the IDs have been added or removed from a list (of objects).
Java's array.hashCode()
does not fit the bill, because even if it is apparently consistent between JVM invocations it returns a different hash if the order of elements changes or if another instance with the same elements is created:
int[] ids1 = {1, 2, 3};
System.out.println(ids1.hashCode());
// output: 980546781
int[] ids1Copy = {1, 2, 3};
System.out.println(ids1Copy.hashCode());
// output: 2061475679
int[] ids2 = {2, 1, 3};
System.out.println(ids2.hashCode());
// output: 140435067
My understanding is that ids1.hashCode()
computes the hash for the memory address of the array and not a cumulative hash code for the primitive elements in the array.
What other approaches could be used in this case apart from hashing each element separately?