aspose file tools*
The moose likes Java in General and the fly likes Reg: HashMap Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Reg: HashMap" Watch "Reg: HashMap" New topic
Author

Reg: HashMap

shyam sunder prasad
Ranch Hand

Joined: Mar 23, 2011
Posts: 62
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

Joined: Apr 06, 2010
Posts: 4422
    
    8

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

Joined: Oct 27, 2005
Posts: 19722
    
  20

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4422
    
    8

Rob Spoor wrote:I think you meant Hashtable, not HashSet.

Good point!
shyam sunder prasad
Ranch Hand

Joined: Mar 23, 2011
Posts: 62
yes that is not HashSet , HashTable i was talking about .
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Reg: HashMap