Sunday, March 13, 2011

Comparison of map and hashtable

If you've used Hashtable, you're already familiar with the general flavor of Map. (Of course Map is an interface, while Hashtable is a concrete implementation.) Here are the major differences:
  • Map provides Collection-views in lieu of direct support for iteration via Enumeration objects. Collection-views greatly enhance the expressiveness of the interface, as discussed later in this lesson.
  • Map allows you to iterate over keys, values, or key-value pairs; Hashtable did not provide the third option.
  • Map provides a safe way to remove entries in the midst of iteration; Hashtable did not.
Further, Map fixes a minor deficiency in the Hashtable interface. Hashtable has a method called contains, which returns true if the Hashtable contains a given value. Given its name, you'd expect this method to return true if the Hashtable contained a given key, as the key is the primary access mechanism for a Hashtable. The Map interface eliminates this source of confusion by renaming the method containsValue. Also, this improves the consistency of the interface: containsValue parallels containsKey nicely.

No comments:

Post a Comment

Chitika