I'm trying to get my head around Maps, and I'm not doing too badly, I am ok with the implementation, and I can envision a few different scenarios where I might put them to use.
But there a few things that just won't fall into place for me. And I'm hoping not to turn it into a full fledged course on interfaces.
OK, The Java Tutorial says: A Map is an object that maps keys to values. No problem, except that we are talking about something that is defined as an interface, and furthermore, it is an interface that is not explicitly implemented. using implements in a class declaration.
So let me start by asking, why is it an interface, and not just another Java class that can be instantiated or extended?
What if it said "A class that maps key to values implements the Map interface." Would that make more sense to you? The idea is that what is said about an interface applies to the classes that implement it.
why is it an interface, and not just another Java class that can be instantiated or extended?
To make it easy for there to be multiple implementations. TreeMap and HashMap come with Java. Both implement the Map interface. Both map keys to values.
OK, The Java Tutorial says: A Map is an object that maps keys to values. No problem, except that we are talking about something that is defined as an interface, and furthermore, it is an interface that is not explicitly implemented. using implements in a class declaration.
What do you mean by "not explicitly implemented"? It's implemented by the Hashtable class, HashMap class, TreeMap class, ConcurrentHashMap class, etc.. Instantiating any of this will give you a Map object.
So let me start by asking, why is it an interface, and not just another Java class that can be instantiated or extended?
It is a class that can be instantiated. In fact, it is many classes that can be instantiated. By using the Map as your reference, you don't care which implementation you instantiate.
Map is an interface because it just defines some methods useful for working with Maps.
There are several classes that do implement java.util.Map, the most common being java.util.HashMap and java.util.TreeMap. They are two different implementations of a Map, but because you can reference them using the Map interface, it doesn't matter which one you actually make. Today you can use a HashMap, tomorrow a TreeMap, and only have to change one line of code:
I just want to say LinkedHashMap, 'cuz I told somebody to use one today.
Carry on.
Fred Hamilton
Ranch Hand
Joined: May 13, 2009
Posts: 679
posted
0
Jeanne Boyarsky wrote:
What if it said "A class that maps key to values implements the Map interface."Would that make more sense to you?
Henry Wong wrote:
What do you mean by "not explicitly implemented"? It's implemented by the Hashtable class, HashMap class, TreeMap class, ConcurrentHashMap class, etc.. Instantiating any of this will give you a Map object.
OK these two points seem to shed some light.
Henry, rather than try to explain my statement, I'm just going to let all this sink in for a day or two. I'm sure things will become more clear. Thanks to all for your comments, some good food for thought here.
And there is a partial Map implementation in AbstractMap. You can extend that if you want to create your own Map implementation but don't want to do all the work.