• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

HashMap and HashTable

 
Ranch Hand
Posts: 207
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
HashMap is not synchronized by default.Why is it not synchronized?
Why HashTable is synchronized?
Why does HashMap allow null value and HashTable not?
:roll:
 
Ranch Hand
Posts: 5093
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
because?

HashMap isn't synchronized because there's already Hashtable if you want a synchronized Map...
 
author
Posts: 3252
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would sort of disagree with that.

Synchronization has been removed in the JDK 1.2 collections because it usually is just extra overhead that doesn't buy you anything:
  • In single-threaded code, you don't need synchronization.
  • In multi-threaded code, you usually do need synchronization, but rarely where Hashtable, Vector and friends provide it. Most of the time, it is not the little collection methods that need to be atomic with respect to other threads, but much coarser-grained operations that you write yourself. The synchronization in Hashtable etc. is then useless, and worse, it may lull inexperienced developers into a false sense of security: their code "is threadsafe because it uses synchronized collections". Not at all.
  • Secondly, I disagree that you would use Hashtable if you did happen to need a map that was synchronized at that level; use Collections.synchronizedMap(). Hashtable, like all the legacy collection classes such as Vector, should be avoided where possible for their synchronization, bloated API, and inferior enumeration behaviour.

    See also this thread.

    - Peter
     
    The fastest and most reliable components of any system are those that are not there. Tiny ad:
    We need your help - Coderanch server fundraiser
    https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
    reply
      Bookmark Topic Watch Topic
    • New Topic