File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes HashMap or HashTable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "HashMap or HashTable" Watch "HashMap or HashTable" New topic

HashMap or HashTable

Swati Gupta
Ranch Hand

Joined: May 28, 2002
Posts: 106
ques from JQplus Sample exam:
Your application needs to load a set of key value pairs from a database table which never changes. Multiple threads need to access this information but none of them changes it.
Which class would you use to store such data?
The options are Hashtable,HashMap,TreeMap,Set,List
and 2 choices
I think the option should be HashMap and HashTable but can we just use HashMap as we do not need to synchronize this so we don't need HashTable.
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
What are you asking?
I thought the question stated that you should choose two answers. If that's the case, then HashMap and Hashtable are the correct answers. The only reason HashMap is a correct answer is because there is no need for synchronization.

SCJP Tipline, etc.
Thiru Thangavelu
Ranch Hand

Joined: Aug 29, 2001
Posts: 219
I read from API that HashMap is not synchronized. It says only HashTable and Vector are synchronized.(Thread Safe). Then what could be the reason to select HashMap?

Thanks,<br />Thiru<br />[SCJP,SCWCD,SCBCD]
Younes Essouabni
Ranch Hand

Joined: Jan 13, 2002
Posts: 479
Multiple threads need to access this information but none of them changes it.

Threads don't change information, so you don't need synchronization.

By constantly trying one ends up succeeding. Thus: the more one fails the more one has a chance to succeed.
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
HashMap is quicker than HashTable.

SCJP2. Please Indent your code using UBB Code
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
Originally posted by Jose Botella:
HashMap is quicker than HashTable.

To elaborate...HashMap is quicker than Hashtable because HashMap isn't synchronized. There is no need for invoking threads to take the extra time to acquire a monitor or to wait for one in the case that it has already been acquired by another thread.
wood burning stoves
subject: HashMap or HashTable
Similar Threads
Using Properties instead of HashMap when dealing with Strings
key-value pair for multiple threads.
Hash tables
BigDecimal nextProbablePrime(). HashMap initial capacity
HashTable vs. HashMap