aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes HashMap and HashSet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "HashMap and HashSet" Watch "HashMap and HashSet" New topic
Author

HashMap and HashSet

dennis zined
Ranch Hand

Joined: Mar 07, 2003
Posts: 330
Hi all. What would be a good scenario where a HashMap is more suited than a HashSet or vice versa.
Also, would this type of question be asked at the exam.
thanks.


SCJP 1.4<br />SCWCD 1.4
Ko Ko Naing
Ranch Hand

Joined: Jun 08, 2002
Posts: 3178
More often than not, you will use a HashSet for storing your duplicate-free collection. For efficiency, objects added to a HashSet need to implement the hashCode() method in a manner that properly distributes the hash codes. While most system classes override the default hashCode() implementation in Object, when creating your own classes to add to a HashSet remember to override hashCode().
As with all the concrete implementations, which implementation you use depends on your specific needs. For inserting, deleting, and locating elements in a Map, the HashMap offers the best alternative. No duplication allowed in HashMap.
Hope the above explanation helps u...


Co-author of SCMAD Exam Guide, Author of JMADPlus
SCJP1.2, CCNA, SCWCD1.4, SCBCD1.3, SCMAD1.0, SCJA1.0, SCJP6.0
dennis zined
Ranch Hand

Joined: Mar 07, 2003
Posts: 330
hi Ko Ko, thanks for the explanation.
Karthik Veeramani
Ranch Hand

Joined: Dec 22, 2002
Posts: 132
What would be a good scenario where a HashMap is more suited than a HashSet or vice versa.

While developing some serious software code, yes, I definitely find HashMap useful in so many places. The purpose of set and map are different, and at some points, u'll definitely need their unique feature.
Consider the Properties class, for instance, which is a map implementation. Thats very useful for maintaining a key-value pair of a property name and its value. You can maintain user preferences in a map like this, and use that object through out ur code. I dont think HashSet would be a wise substitute for a scenario like this.
Also, would this type of question be asked at the exam.

Yeah, u can expect some questions along that line, but not exactly the way u ask.
[ December 16, 2003: Message edited by: Karthik Veeramani ]

Thanks<br />Karthik<br />SCJP 1.4, CCNA.<br /> <br />"Success is relative. More the success, more the relatives."
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: HashMap and HashSet