Re: performance of HashSet with strings and integers



On Oct 7, 10:59 am, Patricia Shanahan <p...@xxxxxxx> wrote:
Pascal Lecointe wrote:
On 7 oct, 16:06, Patricia Shanahan <p...@xxxxxxx> wrote:
The equals method only gets called if the HashSet contains an element
whose hash code is equal to the probe's hash code but that is not the
same object as the probe. That is very unlikely in a test with so few
objects.

Not really. If I remember correctly, the equals is always called
(because same
hash not imply same object). But in this case, the equals first test
that the
references are the same. As the Strings of the example are all in the
constant pool,
the references are the same, and so the equals test is fast.

The match test in HashMap's getEntry is such that either hash code
mismatch or reference equality prevents execution of the equals call.
The implementation of the HashSet contains uses the HashMap containsKey,
which uses getEntry.


Note also that typical implementations of 'equals()' that do not do
mere reference equality do check reference equality first, and only
compare attributes if the references differ. Of course, there's no
guarantee that any particular 'equals()' implementation does this. In
the case of the logic just described, it may well be that reference
equality is checked twice - once by the 'Map' and once by the element
- before value equality is finally invoked.

To review:

HashMap equality checks:
hashCode(): if unequal, no need to continue
reference == : if 'true', no need to continue
value equality: return result

--
Lew
.



Relevant Pages

  • Re: Object equals
    ... I was under the impression that o.equals compares content. ... The default implementation of Equals supports reference equality only, but derived classes can override this method to support value equality. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: How can I "force" an upcast? Convert subclass into superclass?
    ... > habitual implementation of equals that I think Java developers need to ... I would agree on the fact that the equality has to be defined depending ... for any non-null reference value x, ...
    (comp.lang.java.programmer)
  • Re: Why is there no Equals() or GetHashCode() in ArrayList?
    ... want to have a difference meaning for equals then you should implement it. ... For equality, it is very simple. ... contained objects and check if they are "equal". ... define if they should be equal in value or in reference. ...
    (microsoft.public.dotnet.framework)
  • The truth about decibels
    ... equals 20 log x/y, where x and y are the different signal levels]. ... then it must be relative to some 0 dB reference point. ... was labeled dBv but was too often confused with dBV ... +4 dBu Standard pro audio voltage reference level equal to 1.23 Vrms. ...
    (sci.electronics.design)
  • Re: A better cloning mechanism
    ... > So it seems that if equals() were removed from Object, ... that do not need object semantics. ... I have often in the past argued that, in fact, the '==' operator in Java ... answer and you are only doing the reference comparison for performance. ...
    (comp.lang.java.programmer)