Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Reg: HashMap

 
shyam sunder prasad
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,

The difference between HashMap and HashSet is HashSet never accept null keys but why the programmers choose HashMap meagerly.

what is the reason?
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, the difference between HashMap and HashSet is that they're fundamentally different data structures. A HashSet implements the Set interface, and contains a set of unique objects. A HashMap implements the Map interface, and contains key-value pairs (where the key is unique). You use them for different purposes.

The "Hash" in both names is because both use a hashing algorithm (relying on the objects hashCode() method) in their implementation.
 
Rob Spoor
Sheriff
Pie
Posts: 20665
65
Chrome Eclipse IDE Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you meant Hashtable, not HashSet. With Hashtable you get a synchronized map, something that's usually not necessary. And besides, why not allow null keys and values? I think Hashtable is too strict in disallowing them in the first place.

And if you really don't want null keys and values, use Collections.checkedMap. This will throw an exception if a key / value is not of the right class - including if it's null.
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Spoor wrote:I think you meant Hashtable, not HashSet.

Good point!
 
shyam sunder prasad
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes that is not HashSet , HashTable i was talking about .
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic