开发者

Why does HashSet implementation in Sun Java use HashMap as its backing?

开发者 https://www.devze.com 2022-12-20 04:54 出处:网络
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.

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 e开发者_如何学Cntry itself.

But, why is it still used? Is there any reason to use it besides making it easier to maintain the codes?


Actually, it's not just HashSet. All implementations of the Set interface in Java 6 are based on an underlying Map. 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 of Set.

Your main questions are

But, why is it still used? Is there any reason to use it besides making it easier to maintain the codes?

I assume that code maintenance is a big motivating factor. So is preventing duplication and bloat.

Set and Map are similar interfaces, in that duplicate elements are not allowed. (I think the only Set not backed by a Map is CopyOnWriteArraySet, which is an unusual Collection, because it's immutable.)

Specifically:

From the documentation of Set:

A collection that contains no duplicate elements. More formally, sets contain no pair of elements e1 and e2 such that e1.equals(e2), and at most one null element. As implied by its name, this interface models the mathematical set abstraction.

The Set interface places additional stipulations, beyond those inherited from the Collection interface, on the contracts of all constructors and on the contracts of the add, equals and hashCode methods. Declarations for other inherited methods are also included here for convenience. (The specifications accompanying these declarations have been tailored to the Set interface, but they do not contain any additional stipulations.)

The additional stipulation on constructors is, not surprisingly, that all constructors must create a set that contains no duplicate elements (as defined above).

And from Map:

An object that maps keys to values. A map cannot contain duplicate keys; each key can map to at most one value.

If you can implement your Sets using existing code, any benefit (speed, for example) you can realize from existing code accrues to your Set as well.

If you choose to implement a Set without a Map backing, 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.


My guess is that HashSet was originally implemented in terms of HashMap in order to get it done quickly and easily. In terms of lines of code, HashSet is a fraction of HashMap.

I would guess that the reason it still hasn't been optimized is fear of change.

However, the waste is much worse than you think. On both 32-bit and 64-bit, HashSet is 4x larger than necessary, and HashMap is 2x larger than necessary. HashMap could be implemented with an array with keys and values in it (plus chains for collisions). That means two pointers per entry, or 16 bytes on a 64-bit VM. In fact, HashMap contains an Entry object per entry, which adds 8 bytes for the pointer to the Entry and 8 bytes for the Entry object header. HashSet also uses 32 bytes per element, but the waste is 4x instead of 2x since it only requires 8 bytes per element.


Yes you are right, a small amount of wastage is definetley there. Small because, for every entry it uses the same object PRESENT(which is declared final). Hence the only wastage is for every entry's value in the HashMap.

Mostly I think, they took this approach for maintainability and reusability. (The JCF developers would have thought, we have tested HashMap anyway, why not reuse it.)

But if you are having huge collections, and you are a memory freak, then you may opt out for better alternatives like Trove or Google Collections.


I looked at your question and it took me a while to think about what you said. So here's my opinion regarding the HashSet implementation.

It is necessary to have the dummy instance to know if the value is or is not present in the set.

Take a look at the add method

public boolean add(E e) {
return map.put(e, PRESENT)==null;
}

Abd now let's take a look at the put return value

@returns the previous value associated with key, or null if there was no mapping for key. (A null return can also indicate that the map previously associated null with key.)

So the PRESENT object is just used to represent that the set contains the e value. I think you asked why not use null instead of PRESENT. But the, you would not be able to distinguish if the entry was previously on the map because map.put(key,value) would always return null and you would not have way to know if the key existed.


That being said you could argue that they could have used an implementation like this

   public boolean add(E e) {

        if( map.containsKey(e) ) {
            return false;
        }

        map.put(e, null);

        return true;

}

I guess they waste 4 bytes to avoid computing the hashCode, as it could be expensive, of the key two times (if the key is going to be added).


If you question referred to why they used a HashMap that would waste 8 bytes (because of the Map.Entry) instead of some other data structure using a similar Entry of only 4, then yes, I would say they did it for the reasons you mentioned.


I am guessing that it has never turned up as a significant problem for real applications or important benchmarks. Why complicate the code for no real benefit?

Also note, that object sizes are rounded up in many JVM implementation, so there may not actually be an increase in size (I don't know for this example). Also the code for HashMap is likely to be compiled and in cache. Other things being equal, more code => more cache misses => lower performance.


After searching through pages like this wondering why the mildly inefficient standard implementation, found com.carrotsearch.hppc.IntOpenHashSet


Your question: I think that wastes 4 byte (on 32-bit machines) for the size of the entry itself.

Just one Object variable is created for the entire datastructure of hashset and doing that would save yourself from re-writing the entire hashMap kind of code again.

private static final Object PRESENT = new Object();

All the keys are having one value i.e PRESENT object.

0

精彩评论

暂无评论...
验证码 换一张
取 消