This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

benefits of HashMap() over Hashtable

 
kesava chaitanya
Ranch Hand
Posts: 140
  • 0
  • 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: 20836
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 0
  • 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
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic