Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does Hashtable use Entry<?,?> internally?

Here's Hashtable#get:

@SuppressWarnings("unchecked")
public synchronized V get(Object key) {
    Entry<?,?> tab[] = table;
    int hash = key.hashCode();
    int index = (hash & 0x7FFFFFFF) % tab.length;
    for (Entry<?,?> e = tab[index] ; e != null ; e = e.next) {
        if ((e.hash == hash) && e.key.equals(key)) {
            return (V)e.value;
        }
    }
    return null;
}

Why does it use Entry<?,?> instead of Entry<K,V>?

like image 826
user1883212 Avatar asked Oct 23 '15 16:10

user1883212


1 Answers

Hashtable's creation predates any work done with generics in Java 1.5, so the likely scenario here was that the generics were retrofitted.

Although a bigger tell may be due to the fact that table is an array, and generics and arrays just don't get along well.

If table (the field in Hashtable) were typed, you'd have to deal with a lot of these declarations...

// Generic array creation! 
Entry<K, V>[] newMap = new Entry<K, V>[newCapacity];

...and the likely design/implementation decision was to strive for compatibility as opposed to a full-on embrace of generics.

Also note that creating an array type with a wildcard will not cause an compile-time error, whereas creating an array with a concrete type will, due to the fact that a generic type with an unbound wildcard is considered reifiable:

List<?>[] foo = new ArrayList[10]; // perfectly legal but not encouraged
List<String> bar = new ArrayList[10]; // not legal

The convention going forward would be to use HashMap instead, since this particular implementation is both synchronized and still uses a lot of pre-1.5 conventions in it. (If you want synchronization, even the docs recommend ConcurrentHashMap.)

like image 109
Makoto Avatar answered Oct 04 '22 02:10

Makoto