java - About a blocking synchronized collection -


directly api ie collections.synchronizedcollection(collection):

the returned collection not pass hashcode , equals operations through backing collection, relies on object's equals , hashcode methods. necessary preserve contracts of these operations in case backing collection set or list.

does mean if have overridden methods equals , hashcode, overridden methods won't taken account? if yes, why? quite misleading...

suppose equals() delegated backing collection, , suppose backing collection arraylist list.

you have

collections.synchronizedcollection(list).equals(list) == true 

because implemented delegating list.equals(list).

but have

list.equals(collections.synchronizedcollection(list)) == false 

because lists can't equal collections not lists, , collections.synchronizedcollection(list) not list.

this severely break contract of object.equals(). if want preserve equality, use collections.synchronizedlist() synchronize list, , collections.synchronizedset() synchronize set.


Comments