A java.util.HashSet
uses a java.util.HashMap
as its storage. A java.util.HashMap
uses a linked Entry
object to represent the buckets in the map. If you follow through the source code you will get to the contructor of java.util.HashMap.Entry
:
Entry(int h, K k, V v, Entry n)
{
value = v;
next = n;
key = k;
hash = h;
}
From this you can see that new items are added to the start of the bucket (the Entry n
representing the first Entry
of the bucket), so in your case the items in the bucket (there's only a single bucket because the hash code for each Employee
is the same) will be in the order:
Martin -> Peter -> Alex
Hence when adding Alex a second time, each value is checked for equality before getting to Alex.