Looking at the source of Java 6, HashSet<E> is actually implemented using HashMap<E,Object>, using dummy object instance on every entry of the Set.
I think that wastes 4 byte (on 32-bit machines) for the size of the entry itself.
But, why is it still used? Is there any reason to use it besides making it easier to maintain the code?
Actually, it’s not just
HashSet. All implementations of theSetinterface in Java 6 are based on an underlyingMap. This is not a requirement; it’s just the way the implementation is. You can see for yourself by checking out the documentation for the various implementations ofSet.Your main questions are
I assume that code maintenance is a big motivating factor. So is preventing duplication and bloat.
SetandMapare similar interfaces, in that duplicate elements are not allowed. (I think the onlySetnot backed by aMapisCopyOnWriteArraySet, which is an unusual Collection, because it’s immutable.)Specifically:
From the documentation of
Set:And from
Map:If you can implement your
Sets using existing code, any benefit (speed, for example) you can realize from existing code accrues to yourSetas well.If you choose to implement a
Setwithout aMapbacking, you have to duplicate code designed to prevent duplicate elements. Ah, the delicious irony.That said, there’s nothing preventing you from implementing your
Sets differently.