File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes using hashtable as return type Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "using hashtable as return type" Watch "using hashtable as return type" New topic
Author

using hashtable as return type

shriram iyer
Ranch Hand

Joined: Dec 10, 2003
Posts: 43
code snippet is as follows

My question is based on following method



What is the drawback of returning Hashtable as return type.Is there is any drawback as such ?

thanks
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19781
    
  20

If you want to start using a TreeMap you will need to change not only that method, but also all methods that call that method and use Hashtable as reference type. By using Map you can change anytime you want:
If you now need to switch to TreeMap, you only need to change line 3 into this:
All other code will still compile without a problem. And if you really need the Map to be synchronized, just use this:


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
shriram iyer
Ranch Hand

Joined: Dec 10, 2003
Posts: 43
Rob Spoor wrote:If you want to start using a TreeMap you will need to change not only that method, but also all methods that call that method and use Hashtable as reference type. By using Map you can change anytime you want:
If you now need to switch to TreeMap, you only need to change line 3 into this:
All other code will still compile without a problem. And if you really need the Map to be synchronized, just use this:


my question is that,whether there is any drawback in "using hashtable object" as return type
Ralph Cook
Ranch Hand

Joined: May 29, 2005
Posts: 479
I think what the other poster might have meant was that "TreeMap" is a more flexible datatype than HashTable, and therefore might be regarded as a better choice to return. You can write the routine to return a TreeMap and implement it today with HashTable, and it makes it easier to change your implementation later. If you do that, of course, your users are deprived of any methods that are in HashTable but not in TreeMap. Whether this is good for you depends on your application.

HashTable is "synchronized"; this means that, for a given instance of HashTable, if any thread is in the middle of a call to a method of that HashTable (i.e., is in a HashTable method on that instance), then no other thread is allowed to enter any other method on that instance. Synchronized methods are slower than other methods, sometimes much slower. Whether you need or might ever need synchronization or extra speed again depends on your application, etc.

rc
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8398
    
  23

shriram iyer wrote:my question is that,whether there is any drawback in "using hashtable object" as return type

Yes, and I can think of 3 reasons straight away (most of which have already been mentioned):
1. Hashtable is old. These days most people use HashMap.
2. Hashtable is synchronized. Unless you're using it in a multi-threaded situation, you probably don't need it (and even then you need to use it with care), and it will be slower than HashMap.
3. Returning an interface is usually better; and you have 3 to choose from: Map (as already suggested), SortedMap, or NavigableMap. Which one you choose will depend on what you expect your clients to do with the returned object, so you might want to look at the APIs for all 3.(*)

Winston

(*)Edit: And BTW, use the least specific interface that fulfills the need.


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: using hashtable as return type