Win a copy of Testing JavaScript Applications this week in the HTML Pages with CSS and JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Hashtable or HashMap

 
Ranch Hand
Posts: 128
MS IE Eclipse IDE 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.
 
Ranch Hand
Posts: 642
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.
 
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
MS IE Eclipse IDE 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: 642
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.
 
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).
 
author
Posts: 23879
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux 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
 
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.
 
Marshal
Posts: 25682
69
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
MS IE Eclipse IDE Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks everybody for the tips.
 
I've got no option but to sell you all for scientific experiments. Or a tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic