Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Can ArrayList/HashMap be synchronized?

 
amna vijay
Greenhorn
Posts: 4
  • 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
  • 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
  • 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: 20606
60
Chrome Eclipse IDE Java Windows
  • 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
Posts: 21316
32
Eclipse IDE Firefox Browser MySQL Database
  • 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: 20606
60
Chrome Eclipse IDE Java Windows
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic