Also, the fact that �area� is a final variable indicates that its value cannot be changed later. Hence it should not be included in the computation of the hash code value for the objects of this class.
I beleive using finals for hash code is legal yet not being of much help for search speed. Does the explanation say that finals must be excluded from hash calculations?
"I'm not back." - Bill Harding, Twister
Marlene, where did you hear that finals should be excluded from hashcode computations? I'm guessing that wherever it was, they really meant final static fields. If a value is the same across the entire class, it probably isn't very useful for the hashcode. Though it may be in some circumstances, e.g. where your HashMap has objects from a lot of different classes. Or even more unlikely scenarios. In general though I'd agree with those who say that whether a field is final should probably be ignored when computing hash codes. Actually, immutable objects are often better has map keys anyway, since if someone changes a mutable field, that will change the hash code, which changes where the key shoul be found in the internal hashmap structure, exceptthat most likely the HashMap won't actually recompute the hashCode() since it just assumes that the hashCode() will remain constant... You can avoid this by making a point of not changing objects which are used as keys, to avoid this confusion. But it's worthwhile to try using immutable objects as your keys in the first place.
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime. |