If two objects are equal, they must have same hash code. But it does not imply the other way too. Two objects having the same hash code may be not equal (see, hash collision). So this approach may give false positives.
- Abhinav On Thu, Aug 5, 2010 at 1:45 PM, Nicolas Oury <[email protected]> wrote: > That's might be an interesting trade-of to try. > It is really good when big data structures are nearly equal. The > comparison is linear but the comparaison once you know the hash is > O(1) with a high probability. > It is bad on very different structure, if you force a hash. > Ideally, the equals method could hash as it goes. When 2 structures > are equal, their hashes would be known. > If not, by comparing the numbero fo elments hashed to the count, the > implementation could decide whether or not to continue hashing... > > On Wed, Aug 4, 2010 at 10:40 PM, sune.simonsen <[email protected]> > wrote: > > I was looking around the Clojure source code and noticed that the > > persistent immutable collections caches their hash codes. > > > > The java doc says the following about the equals method: (http:// > > download.oracle.com/javase/1.4.2/docs/api/java/lang/Object.html) > > "If two objects are equal according to the equals(Object) method, then > > calling the hashCode method on each of the two objects must produce > > the same integer result." > > > > So I was thinking, wouldn't it be possible to use the cached hash code > > to rule out cases of equals where equals returns false? > > > > public boolean equals(Object obj){ > > ... > > if (_hash != -1 && hashCode() != obj.hashCode()) { > > return false; > > } > > ... > > } > > > > This might of cause result in an extra call to hashCode(), so I don't > > know if it is worth it. > > Maybe some internal functionality could be added, to ask the > > collections if their hash code has been cached. > > > > What are your thoughts? > > > > Kind regards Sune Simonsen > > > > -- > > You received this message because you are subscribed to the Google > > Groups "Clojure" group. > > To post to this group, send email to [email protected] > > Note that posts from new members are moderated - please be patient with > your first post. > > To unsubscribe from this group, send email to > > [email protected]<clojure%[email protected]> > > For more options, visit this group at > > http://groups.google.com/group/clojure?hl=en > > -- > You received this message because you are subscribed to the Google > Groups "Clojure" group. > To post to this group, send email to [email protected] > Note that posts from new members are moderated - please be patient with > your first post. > To unsubscribe from this group, send email to > [email protected]<clojure%[email protected]> > For more options, visit this group at > http://groups.google.com/group/clojure?hl=en > -- You received this message because you are subscribed to the Google Groups "Clojure" group. To post to this group, send email to [email protected] Note that posts from new members are moderated - please be patient with your first post. To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/clojure?hl=en
