File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes General questions about Map interface. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "General questions about Map interface." Watch "General questions about Map interface." New topic
Author

General questions about Map interface.

Fred Hamilton
Ranch Hand

Joined: May 13, 2009
Posts: 679
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

Joined: May 26, 2003
Posts: 31069
    
232

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.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 19068
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 19068
    
  40


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

Henry
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

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
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

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

Joined: Sep 29, 2008
Posts: 12617

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
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

Joined: Oct 27, 2005
Posts: 19785
    
  20

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: General questions about Map interface.