Java – hashcode and equals are collections unmodifiableCollection()

Collections class has some static help methods to provide read-only views of various collection types, such as unmodifiableset(), unmodifiablelist(), etc For these view objects, the hashcode () and equals () methods forward calls to the underlying collection... With a strange exception: unmodifiablecollection()

JavaDoc explicitly states:

My question: if the background collection is a collection or list, I want the behavior to be consistent with unmodifiableset() and unmodifiablelist() How to breach a hashcode / equal contract?

Solution

From Javadoc for collection:

An unmodifiablelist is an unmodifiablecollection, but the opposite is not the opposite - an unmodifiablecollection wraps a list, not an unmodifiablelist Therefore, if an unmodifiablecollection containing list a is compared with an unmodifiablelist containing the same list a, the two wrappers should not be equal If you just pass the packing list, they will be equal

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>