This week's book giveaway is in the OCMJEA forum.
We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Collections.synchronizedMap Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Collections.synchronizedMap" Watch "Collections.synchronizedMap" New topic
Author

Collections.synchronizedMap

Seema Kekre
Ranch Hand

Joined: Dec 02, 2010
Posts: 35


I was wondering what concrete class is returned by Collections.synchronizedMap(hm) on line 5. It does not seem to be Hashtable because the last line prints null and Hashtable does not allow null values. The iterators returned by both Collections.synchronizedMap() and Hashtable are fail-fast. How is the Hashtable different from the class returned by Collections.synchronizedMap() (other than the null) and how is the later better wrt performance?


OCPJP 6
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

The documentation doesn't explicitly say so, but my guess is that Collections.synchronizedMap(X) allows null values if and only if X allows null values. After all, the map you get is just a synchronized wrapper for the original map.

So, that being the case, all of your questions are off-track.

Knowing the name of the class returned by Collections.synchronizedMap isn't going to help, because it isn't going to be a publicly documented class anyway. You could put this into your code:

and that would answer your question, but it wouldn't tell you anything.

And how does Hashtable differ from the class returned by Collections.synchronizedMap? Exactly in the same way that it differed from the original map, except for the synchronization of course.

As for how Collections.synchronizedMap is better for performance, well, it's thread-safe whereas the original map presumably wasn't. This will perform better in situations where you need thread-safety, in the sense that it will behave properly whereas the original map wouldn't behave properly. Objects which don't behave properly have really bad performance in my opinion.

Hope that helps, I think you were assuming something more than what is actually there.
Seema Kekre
Ranch Hand

Joined: Dec 02, 2010
Posts: 35
Thanks Paul. For the second part, I meant to ask how Collections.synchronizedMap is better for performance compared to Hashtable.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Well, that depends on what you mean by "performance". Is there a particular reason why you expected one to be better on whatever measure you had in mind? Or why you thought it was useful to compare the two?

Edit: Let me clarify that. It doesn't make sense to talk about the "performance" of a class. It does make sense to talk about the "performance" of a certain process. For example, you might discuss how long it takes to add an entry to various kinds of map, or how to get an entry from various kinds of map. Or you might discuss the memory requirements of various kinds of map. But if you've been reading things like "Hashmap is better than Hashtable because it isn't synchronized", you should understand that statements like that are oversimplified. They are just slogans which (as slogans are designed to do) prevent you from thinking about the issues properly. The area of performance is more complicated than that.
Seema Kekre
Ranch Hand

Joined: Dec 02, 2010
Posts: 35
I do not have any specific requirements in mind. I have just been curious about why would someone use SynchronizedMap over Hashtable. Hashtable being an old implementation may not have the improvements that were done for SynchronizedMap . What I am trying to understand is what were the improvements done for SyncronizedMap that are different than Hashtable. I do not want to blindly believe that Hashtable is bad (which is what I have been reading in a lot of forums), I want to know what is so wrong with Hashtable that it is being shunned , what scenarios did it behave badly under (compared to SynchronizedMap )

Just like with the ConcurrentHashMap being better suited in situations where you need to do more insertions but not frequent read operations since it does not do a complete block, while SynchronizedMap creates a blocking Map which is better for up-to-date views but slower for insertion. (Correct me if this is wrong). I have not found a similar explanation comparing Hashtable and SynchronizedMap. I hope I am not too vague in my question.

Edit: I should have said Collections.syncronizedMap(new HashMap()) vs Hashtable. Now both of them have synchronized methods, both have fail-fast iterators and both are Map implementations. If I am not using null keys or values, what advantage is Collections.syncronizedMap(new HashMap()) offering me over Hashtable.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Collections.synchronizedMap