collections - Comparing java maps via hashing -


i want compare 2 java maps simple hash.

each object on different computer, sending hash on network cheaper sending whole object compare.

for example have 2 hashmaps of exampleclass

map<string,exampleclass> one=new ...;  map<string,exampleclass> other=new ...; 

i don't need sure elements equal, it's enough me trust in hash.

i iterate @ each side , create "homemade hash", send network compare example int or something.

it great if "hash" calculated every time object added or deleted collection, saving me iterate whole object. have encapsulate every add/delete of map. there java library this?

if classes implement hashcode() (does not use "default" memory address hashcode) can use map's hashcode().

the caveat here if exampleclass not implement hashcode(), equal items might have different hashes on 2 different machines, result in different hashes maps.


to clarify:

map implements hashcode() defined sum of it's map.enytry's hashcode()s.

map.entry's hashcode() defined xor of key's hashcode() , value's hashcode(). keys strings -- have defined hashcode() (two equal strings have same hashcode()). values exampleclass instances -- also need well-defined hashcode().

in summary, map contains { s1 -> ec1, s2 -> ec2 } have hashcode equal to:

(s1.hashcode() ^ ec1.hashcode()) + (s2.hashcode() ^ ec2.hashcode()) 

meaning depends on exampleclass's hashcode().

if exampleclass did implement hashcode() in way equal exampleclasses give equal hashcode()s, work well. if exampleclass did not implement hashcode(), use object's hashcode(), give different hashcodes().


Comments

Popular posts from this blog

c# - How to set Z index when using WPF DrawingContext? -

razor - Is this a bug in WebMatrix PageData? -

visual c++ - Using relative values in array sorting ( asm ) -