The fastest way to compare two data structures in java

I would like to know what is the fastest way in java 1.5 to compare two data structures.

My data structure is a tree that can be quite large. I can go through the entire data structure and compare 2 node to node (which I think will be slow). Or can I calculate the hash of the data structure to make it faster, right?

What is the best (efficient and not too long) way to calculate this hash?

I would not want too much time to calculate the hash ...

I hope I understand .. :-) ...

+5
source share
7 answers

Each object inherits .equals()and .hashCode()from the Object .

Java .hashCode() ( , ).

, hash collisions , .

, , . , , O(n) , n - .

+1

, - , ? , hashCode .

, - , . - , , , .

+2

, . node -. , a String node, . node .

, - (, ), . , , - , .

, , , - . , , .

+1

gdm, hashCode, , ( , , ). xor () node.hashCode , :

this.hashcode ^= nodeInQuestion.hashCode;

immutable, intern. , , , . , , , , , .

+1

, , .

+1

- compareTo. ( hashcode equals) POJOS.

, , , , . , .

, (Netbeans ).

compareTo , , : TreeMaps, ..

+1
public void preOrderTraversal(Node r1, Node r2) {

       if (r1 != r2 )  { // implement equals here !!  

           System.exit(0); // exit and print not equal
       }

       preOrderTraversal(r1.left(),r2.left());
       preOrderTraversal(r1.right(),r2.right());
}
+1

All Articles