jQuery in Action, 3rd edition
The moose likes Beginning Java and the fly likes Map and TreeMap Instantiation Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Map and TreeMap Instantiation" Watch "Map and TreeMap Instantiation" New topic

Map and TreeMap Instantiation

kalpana Kumar
Ranch Hand

Joined: Jul 03, 2003
Posts: 65
Hi all,
Basic Question:
I want to know the advantages in instantiating the TreeMap as
A. Map testmap = new TreeMap();
B. TreeMap testmap = new TreeMap();
I am just trying to use TreeMap but when I see the examples, I see most of them have used A for getting an instance.
I would like to know the exact details as to why it is done so and how different is it from 'B'.

Nathaniel Stoddard
Ranch Hand

Joined: May 29, 2003
Posts: 1258
Well, it's complicated ... kinda. Basically, the idea is to limit the knowledge of the actual type of object, but instead restrict it to some lesser type. In this case, the rest of your program doesn't really need to know it's actually a TreeMap, so it's typed as a Map instead.

The official OO word is "coupling". By reducing the coupling between your program and the type of the object, it makes other things in software development easier. For instance, what would happen if you suddenly find that a TreeMap doesn't suit your purposes?

Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
I know this question has been asked here before, but I'm having trouble finding the posts with the Search tool. I was hoping to give a link for you, but I guess I'll just let you experiment with keywords for a search. If I find anything, I'll let you know.


Java API Documentation
The Java Tutorial
I agree. Here's the link: http://aspose.com/file-tools
subject: Map and TreeMap Instantiation
jQuery in Action, 3rd edition