File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Can ArrayList/HashMap be synchronized?

 
amna vijay
Greenhorn
Posts: 4
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 171
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not sure but I think it's just for backwards compatibility
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Rob Spoor
Sheriff
Pie
Posts: 20393
47
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Paul Clapham
Sheriff
Pie
Posts: 20203
26
MySQL Database
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20393
47
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
They wouldn't have to, they'd just have to turn off deprecation warnings.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic