4

2つの同一の変換により、同じ。を持つオブジェクトが生成されることを期待していますhashCode。このプロパティを使用して、オブジェクトが意味のある方法で変更されたかどうかを確認したいと思います。

残念ながら、GuavaTransformedCollection extends AbstractCollectionは(とは異なりAbstractList)実装hashCodeまたは同等でTransformedCollectionはなく、そのような試み自体も行いません。

  • イテレータの順序などによって返される値に基づいてを計算することはできませんhashCodeか?
  • それとも、それでも同一性を保証するものではありませんhashCodesか?
  • おそらく、この問題を解決TransformedCollectionできない方法で解決できるAbstractCollectionでしょうか。
4

1 に答える 1

11

Unfortunately, there's no sane way for defining Collection.hashCode. A collection can be a Set or a List (or something else) and the two define hashCode in an incompatible way.

Moreover, for the same reason there's no sane definition for transformedCollection1.equals(transformedCollection2). It could either ignore the order, or not (Set or List semantics). Even worse, the returned Collection is just a view, and such equals would be terrible inefficient.

I'd suggest to use something like ImmutableList.copyOf(transformedCollection) and work with it.

于 2012-08-23T14:51:39.570 に答える