Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

using hashtable as return type

 
shriram iyer
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20526
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:
 
shriram iyer
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 479
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 10417
63
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic