It's not a secret anymore!*
The moose likes Beginning Java and the fly likes Can ArrayList/HashMap be synchronized? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Can ArrayList/HashMap be synchronized?" Watch "Can ArrayList/HashMap be synchronized?" New topic
Author

Can ArrayList/HashMap be synchronized?

amna vijay
Greenhorn

Joined: May 09, 2008
Posts: 4
Was asked the following question in an interview:

Can we synchronize ArrayList or HashMap?
(I said we can - correct me if I am wrong)
If ArrayList and HashMap can be synchronized using synchronized keyword, then what is the use of having Vector and Hashtable?
Manuel Leiria
Ranch Hand

Joined: Jul 13, 2007
Posts: 171
I'm not sure but I think it's just for backwards compatibility


Manuel Leiria<br /> <br />--------------<br />Peace cannot be kept by force; it can only be achieved by understanding. <br /> Albert Einstein
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

Have a look at Collections.synchronizedList() for an answer (though the method name probably gives you a huge clue ).


then what is the use of having Vector and Hashtable?

They are needed for legacy code. A better question is what is the point in using these for any new code?


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19679
    
  18

I still wonder why these classes are not deprecated. And even if the classes are not, then why the "renamed" methods (e.g. addElement, elementAt) are not.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Originally posted by Rob Prime:
I still wonder why these classes are not deprecated. And even if the classes are not, then why the "renamed" methods (e.g. addElement, elementAt) are not.
There are several classes in Swing which use Vector internally (and in their public interfaces too). Perhaps Sun didn't want to change those classes, not even to use the methods from the List interface.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19679
    
  18

They wouldn't have to, they'd just have to turn off deprecation warnings.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Can ArrayList/HashMap be synchronized?