Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Hashtable or HashMap  RSS feed

 
Allen Bandela
Ranch Hand
Posts: 128
Eclipse IDE MS IE Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a JSP that uses a bean to get a Hashtable and then access its containsKey(key) method several times . Would it harm , if I replaced the Hashtable with HashMap, as I want to increase the performance of the jsp, and as 'containsKey()' is synchronized in Hashtable. Please advise.
 
Jignesh Patel
Ranch Hand
Posts: 626
Mac
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First of all changing from Hashtable to Hashmap it improves your performance drastically.

Now, what point to be considered to change from Hastable to HashMap:

Does your Hashtable contains data which constanly keep changing,

If no go ahead and change it,

if yes, is there any problem to show some steal data for a moment to other user while server or another user updating hashMap.

If no, go ahead and change it.
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jignesh Patel:
First of all changing from Hashtable to Hashmap it improves your performance drastically.


Really? Allow me to be skeptical.

The original poster said that the rendering of a particular JSP accessed the map "several" times. Unless by "several", he meant "millions", or the map is absolutely gigantic, the time taken for map access will be completely swamped by the other processing that is done to render a JSP.

That said, it is still a best to use HashMap, not Hashtable, in the vast majority of situations.
 
Allen Bandela
Ranch Hand
Posts: 128
Eclipse IDE MS IE Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Peter Chase:

the time taken for map access will be completely swamped by the other processing that is done to render a JSP.


The page accesses the hashtable around 5 times in the page. Can you suggest some factors that affect the rendering of a JSP page. Now though this has become a JSP question, I still want to know if you have any ideas.
Would lot of scripting decrease the performance ? Would EL help?
[ March 15, 2007: Message edited by: Sylvester James ]
 
Jignesh Patel
Ranch Hand
Posts: 626
Mac
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Really? Allow me to be skeptical.


Yes even it is used for accessing millions of times, still there is a considerable difference to access Hashtable compare to Hashmap.

And when I am talking for performance, it is in the context of replacing Hashtable to HashMap, not in the context of how the business logic gets executed.
 
steve souza
Ranch Hand
Posts: 862
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your problem doesn't sound like a Hashtable vs HashMap issue. As a previous poster said if you are having performance problems it surely doesn't have to do with that choice. you need to measure your codes performance and find out where the real bottleneck is. Random Micro-tunings such as this are almost always a waste of time.

Your issue is probably external IO (perhaps your database), but you will have to start timing your code to find that out. See the javaranch FAQ link on my signature for a few suggestions on Performance and tuning.
 
steve souza
Ranch Hand
Posts: 862
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Note you can use jamon to measure the performance difference between your HashMap and Hashtable. Read the java ranch faq or the jamon site to see how it works in detail. In general you wrap your Object in the jamon monitoring class and all methods will be timed.



That's it. Now all methods called on map will be monitored. I think you will find that Map is not the performance bottleneck.

Here is another link on the topic.

http://www.javaranch.com
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jignesh Patel:
Yes even it is used for accessing millions of times, still there is a considerable difference to access Hashtable compare to Hashmap.


No. I said if it were executed millions of times, there could be a noticeable performance difference. But in this application it is executed about 5 times. In that situation, there would be no noticeable performance difference.

Originally posted by Jignesh Patel:

And when I am talking for performance, it is in the context of replacing Hashtable to HashMap, not in the context of how the business logic gets executed.


I don't know what this sentence is trying to say, but the original question was about whether performance of the JSP would be improved by replacing Hashtable by HashMap. The answer is that it won't - not by any noticable amount.

Using HashMap instead of Hashtable is almost always right in new code. Sometimes, it is worthwhile replacing it in old code. Sometimes, there could be a performance improvement by doing so, but not if there are only 5 map accesses within a complex operation like rendering a JSP. Sometimes, there are other reasons to replace it; perhaps the built-in synchronisation is causing problems (other than performance).
 
Henry Wong
author
Sheriff
Posts: 23283
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sometimes, there are other reasons to replace it; perhaps the built-in synchronisation is causing problems (other than performance).


I agree with Peter here. Synchronization is hardly a good reason to switch to hashmap. Keep in mind that synchronization has greatly improved since Java 5. Prior to that, it mostly suffered with "contended" locks -- and if you have contention in your locks, removing synchronization is a bad idea ...

Henry
 
Rahul Bhattacharjee
Ranch Hand
Posts: 2308
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Sylvester James:
I have a JSP that uses a bean to get a Hashtable and then access its containsKey(key) method several times . Would it harm , if I replaced the Hashtable with HashMap, as I want to increase the performance of the jsp, and as 'containsKey()' is synchronized in Hashtable. Please advise.


I think replacing a HashTable with HashMap would greatly increase perfomance , but perfomance is not the only thing that we need to take care.We also need to see whether context of the map implementation we are talking about is thread safe or not thread safe.If this map is a shared object for multiple threads, then we should be using HashTable.One more thing is that even if this map is shared by many therads and only read operation is used , then its better to go for HashMap as read/get() operation would not cause any inconsistencies.
 
Paul Clapham
Sheriff
Posts: 22487
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Unfortunately as in all questions of synchronization, you have to understand how your application works. Are multiple threads going to be accessing the same hashmap at the same time? If so, are they potentially going to be modifying its contents?

You should answer these questions first. You shouldn't be fiddling with performance changes that could cause your application to work incorrectly.
 
Allen Bandela
Ranch Hand
Posts: 128
Eclipse IDE MS IE Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks everybody for the tips.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!