I would like to hash a large number of int[] in Java. My arrays contain paths from the root to nodes in a tree (nodes are numbered), thus they are very similar on the first values (as all paths come from the root).
I am using Google Guava Hasher, I add each value of my array to the hasher to get the hashCode :
HashFunction hashFunction = Hashing.murmur3_128();
Hasher hasher = hashFunction.newHasher();
for (int i: myArray) {
hasher.putInt(i);
}
int hashCode = inthasher.hash().asInt();
I would like to avoid hashing the whole array again for every path and only hash the last values by adding the last values to a copy of my hasher. Like this :
anotherHasher = hasher.clone();
anotherHasher.putInt(someInt);
int hashCode = hasher.hash().asInt();
int anotherHashCode = anotherHasher.hash().asInt();
But the clone method doesn't exist for Hasher.
Does this kind of copy would save computation time? Is it possible to implement Cloneable with a wrapper containing a Hasher even if the later is not cloneable? If yes how? Is there another way?
EDIT: For the records, what takes times in hashing with Guava is not adding elements with hasher.putInt(someInt) but the hashing itself called at the end with hasher.hash(). So even deep copying was fast (which is not, see the answer by Tomasz Linkowski), my approach is not relevant and does not deserve to be studied further.