what is the difference between a map and a hashmap?
Duy Nguyen
2012-05-13 19:39:02 UTC
I'm new to CS, please give one or two examples.
Also, how would you iterate thru or sort a hashmap using Java's API ?
Any help is appreciated
Three answers:
godfatherofsoul
2012-05-14 12:52:30 UTC
java.util.Map is a generic Collections supertype for any key/value class. It is an interface that only describes behavior. HashMap is a specific implementation of Map that you can directly use. TreeMap is another Map implementation as is any other class that implements the java.util.Map interface.
Whenever you see someone referring to a Map generically, it usually means that the specific implementation isn't important or it is hidden from you.
?
2012-05-14 02:50:27 UTC
There is no difference between the objects. There is a difference in the interface you have to the object. In the first case, the interface is HashMap, whereas in the second it's Map. The underlying object, though, is the same.
The advantage to using Map is that you can change the underlying object to be a different kind of map without breaking your contract with any code that's using it. If you declare it as HashMap, you have to change your contract if you want to change the underlying implementation.
See toutorail http://www.java-samples.com/showtutorial.php?tutorialid=369