This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes benefits of HashMap() over Hashtable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "benefits of HashMap() over Hashtable" Watch "benefits of HashMap() over Hashtable" New topic
Author

benefits of HashMap() over Hashtable

kesava chaitanya
Ranch Hand

Joined: Aug 15, 2001
Posts: 140
hi

this is our EJBHomeFactory class ;we have implemented singleton pattern for this;
my doubt is

synchronizing the hashmap using Collections.synchronizedMap ,

i can achieve this functionality using Hashtable also;

like this
Hastable ht = new Hastable();

by default hashtable gives synchronization; why they have written Collections.synchronizedMap(new HashMap());

whta benefits will it give?

homes = Collections.synchronizedMap(new HashMap());

public class EJBHomeFactory
{
private Map homes;
private static EJBHomeFactory singleton;
private Context ctx;

private EJBHomeFactory() throws NamingException
{
homes = Collections.synchronizedMap(new HashMap());
ctx = new InitialContext();

}

public static EJBHomeFactory getInstance() throws NamingException
{
if (singleton == null)
{
singleton = new EJBHomeFactory();
}
return singleton;
}
}
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18914
    
  40

by default hashtable gives synchronization; why they have written Collections.synchronizedMap(new HashMap());

whta benefits will it give?


It has to do with history... The hashtable map was available right from the beginning -- and it was thread safe. With java 1.2, they added a huge set of collections, and decided to let the developer decide whether it should be synchronized -- hence, the collections class. They can't change the fuctionality of hashtable, or older programs will break.

Besides, there are different types of hashmap classes, tree, sorted, etc., that can be synchronized by the collections class.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
 
Don't get me started about those stupid light bulbs.
 
subject: benefits of HashMap() over Hashtable