• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

benefits of HashMap() over Hashtable

 
kesava chaitanya
Ranch Hand
Posts: 140
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 21021
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic