Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

General questions about Map interface.

 
Fred Hamilton
Ranch Hand
Posts: 684
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34973
379
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A Map is an object that maps keys to values.

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.
 
Henry Wong
author
Marshal
Pie
Posts: 21504
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

Henry
 
Henry Wong
author
Marshal
Pie
Posts: 21504
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Hmmm.... The topic is unanswered for 25 minutes. I post -- and get beaten by 30 seconds...

Henry
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:

or

Any code that uses myMap doesn't care.
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:
Hmmm.... The topic is unanswered for 25 minutes. I post -- and get beaten by 30 seconds...

Henry


Haha. And 3 posts before I get mine in :-(
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I just want to say LinkedHashMap, 'cuz I told somebody to use one today.

Carry on.
 
Fred Hamilton
Ranch Hand
Posts: 684
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Rob Spoor
Sheriff
Pie
Posts: 20667
65
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic