aspose file tools*
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
Author

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

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


Nathaniel Stodard<br />SCJP, SCJD, SCWCD, SCBCD, SCDJWS, ICAD, ICSD, ICED
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.

Layne


Java API Documentation
The Java Tutorial
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Map and TreeMap Instantiation
 
Similar Threads
Map mock question
Hashmap in java
Bubble Sort Set using Comparator
adding a bunch of list to a set
Problem with HashMap