I don't understand this code Map<E, Integer> d = new HashTable<E, Integer>(list.size());
: we create a new object but is it a map or a hashtable? what is the difference between the both of them? I thought that a map is just a way to put 2 element together like a key and its value (for exemple {3; Detroit})

- 11
- 4
-
`Map` is an interface; `Hashtable` is an implementation of this interface (and so is `HashMap` which you should use instead) – fge Apr 26 '15 at 13:12
-
Are you asking what is the difference between an interface and an implementing class? – Brett Okken Apr 26 '15 at 13:12
-
1Related: http://stackoverflow.com/questions/1348199/java-hashmap-vs-map-objects – T.J. Crowder Apr 26 '15 at 13:12
-
yes, what is a difference between an interface and an implementing class? – christine jakotte Apr 26 '15 at 13:14
-
@christinejakotte I think you might benefit from reading the following. The sub-links "What is Inheritance" and "What is an Interface" would be particularly helpful, I think. http://docs.oracle.com/javase/tutorial/java/concepts/ – MadConan Apr 26 '15 at 13:24
3 Answers
is it a map or a hashtable?
Yes.
The static, compile time type of the reference is Map
. As others have already pointed out, it's an interface. You can call all the methods on the Map
interface and know that they'll obey the contract and behave as describe.
The dynamic, run time type of the object reference refers to is Hashtable
. It implements all the methods in the Map
interface in its own way.
The key idea is that the compile time type of a reference is separate from the run time type of the object on the heap that it points to.
Hashtable
is a JDK 1.0 class that sticks around for compatibility reasons. It's been retrofitted to implement the Map
interface, which was introduced later. You'd be well advised to choose another implementation, such as HashMap
, depending on your requirements.
The last part of Hashtable
contains the reason why it should not be used:
As of the Java 2 platform v1.2, this class was retrofitted to implement the
Map
interface, making it a member of the Java Collections Framework. Unlike the new collection implementations,Hashtable
is synchronized. If a thread-safe implementation is not needed, it is recommended to useHashMap
in place ofHashtable
. If a thread-safe highly-concurrent implementation is desired, then it is recommended to useConcurrentHashMap
in place ofHashtable
.
This means that it is less efficient than HashMap
for single-thread models and less efficient than ConcurrentHashMap
for multi-threaded models.
Understanding how compile and run time types differ is crucial to understanding how object oriented polymorphism works. This is true for all OO languages: C++, Java, .NET, Python, etc.

- 305,152
- 44
- 369
- 561
Map
is an interface. Hashtable
is one of the classes that implements the Map
interface.
See the Java Doc for the Map
interface. Specifically the section that says all known implementing classes.
Any class that implements a Map provides a key->value
data-structure. A Map
being an interface defines the contract that all implementing classes must adhere to. By itself, a Map
cannot be instantiated.
Note that while Hashtable
should ideally have been named as HashTable
following the java naming conventions, this is is a pre-historic class in Java which exists even before the standard java naming conventions came into existence. Therefore, it is still called Hashtable
and not HashTable
as wrongly mentioned in your question.

- 15,069
- 6
- 45
- 82
Map
is an interface. HashTable
is one implementation of that interface. There are several others, such as HashMap
, SortedMap
, etc. The interface defines the programming API; the implementation defines how that API is implemented. Different implementations may have different runtime performance characteristics.
With regard to interfaces vs. implementations, you may find my answer to Java - HashMap vs Map objects here helpful.

- 1
- 1

- 1,031,962
- 187
- 1,923
- 1,875