Why doesn't TransformedCollection implement hashCode?

I expect two identical conversions to result in objects with the same hashCode . I would like to use this property to check if my object has changed in a meaningful way.

Unfortunately, Guava TransformedCollection extends AbstractCollection , which (unlike AbstractList ) does not implement hashCode or equal, and TransformedCollection does not make such an attempt.

  • Could not calculate hashCode based on the values โ€‹โ€‹returned by the iterator order, or some such?
  • Or does this not guarantee an identical hashCodes ?
  • Perhaps we can solve this problem for the TransformedCollection so that it is not solved for the AbstractCollection ?
+4
source share
1 answer

Unfortunately, it makes no sense to use Collection.hashCode . A collection can be Set or List (or something else), and the two define hashCode incompatible way.

Also, for the same reason, there is no proper definition for transformedCollection1.equals(transformedCollection2) . It can either ignore the order or not (semantics of Set or List). Worse, the returned Collection is just a view, and such equals would be terribly ineffective.

I would suggest using something like ImmutableList.copyOf(transformedCollection) and working with it.

+11
source

All Articles