Domanda di colloquio di Google

What's the difference between a hashtable and a hashmap?

Risposte di colloquio

Anonimo

28 set 2009

In the general sense, there's no difference in a hashtable and a hashmap. There are, however, differences between the two Java classes named "Hashtable" and "HashMap" You'd need a real programmer to explain the key differences between the two. But, at first glance, it looks to me like one module, Hashtable is meant for use in a multi-thread context, while HashMap is not. For what it is worth, like I said, I ain't a Java programmer, but I did sleep at a Holiday Inn last night (not really).

1

Anonimo

8 nov 2009

I'd assume this question is asked in Java context. There are 3 differences: 1. Hashtable is completely synchronized. There is no concurrent access allowed but it is thread-safe in the sense that only a single thread can access the table. HashMap is not thread-safe in any ways. 2. Hashtable does not allow null keys or null values(you will get a NPE). HashMap allows both null keys and values. 3. Hashtable's enumerator is not fail safe, meaning if the map is changed during iteration, there is no way of knowing so. HashMap's iterator is fail safe, if the map is modified during iteration, you will get a ConcurrentModificationException.

1