简体   繁体   中英

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> ?

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 .)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM