wood burning stoves 2.0
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Dan's questions Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Head First Android this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Dan Watch "Dan New topic

Dan's questions

Kristof Camelbeke
Ranch Hand

Joined: Nov 28, 2001
Posts: 97
Question 11
• Each element must be unique.
• Duplicate elements must not replace old elements.
• Elements are not key/value pairs.
• Accessing an element can be almost as fast as performing a similar operation on an array.
Which of these classes provides the specified features?
a. LinkedList
b. TreeMap
c. TreeSet
d. HashMap
e. HashSet
f. LinkedHashMap
g. Hashtable
h. None of the above
answer = e
But a HashSet implements a Set which cannot contain duplicate values so the second statement "duplicate elements must not replace old elements" is not true for a Hashset because it can't contain duplicate values!!! So how come this is the correct answer ?
[ January 07, 2004: Message edited by: Kristof Camelbeke ]
[ January 07, 2004: Message edited by: Kristof Camelbeke ]
Thomas De Vos
stable boy
Ranch Hand

Joined: Apr 12, 2003
Posts: 425
I think Dan must have meant that when you add a duplicate item then the item that is last added to the collection will not replace the already existing item in the collection.

Try your free <a href="http://www.javacertificate.com" target="_blank" rel="nofollow">SCJP 1.4</a> certification centre.<br />Try your free <a href="http://www.j2eecertificate.com" target="_blank" rel="nofollow">SCWCD</a> certification centre.<br />Try your free <a href="http://www.ejbcertificate.com" target="_blank" rel="nofollow">SCBCD</a> certification centre.<br />Try your <a href="http://www.webspherecertificate.com" target="_blank" rel="nofollow">Websphere (Test 285) </a> certification centre.<br />Try your <a href="http://www.j2mecertificate.com" target="_blank" rel="nofollow">SCMAD</a> certification centre. (New)<br /> <br /><a href="http://blogs.javacertificate.com" target="_blank" rel="nofollow">Java/J2EE Certification Blogging</a>
Kristof Camelbeke
Ranch Hand

Joined: Nov 28, 2001
Posts: 97
Hello Thomas... So this means that you cannot use a HashMap because you cannot have duplicate items, right ?
Yosi Hendarsjah
Ranch Hand

Joined: Oct 02, 2003
Posts: 166
Let me explain with this code:

i1 and i2 are two equal objects. We can successfully add i1 to the HashSet because the Hashset doesn't contain an object that is equal to i1 (it's empty anyway). But we can't add i2 to the HashSet because it already contains i1. And i1 stays in the HashSet.
[ January 07, 2004: Message edited by: Yosi Hendarsjah ]
[ January 07, 2004: Message edited by: Yosi Hendarsjah ]
Kristof Camelbeke
Ranch Hand

Joined: Nov 28, 2001
Posts: 97
ok Yosi I got it... You can't add a duplicate item.
Think I misunderstood the phrasing
Dan Chisholm
Ranch Hand

Joined: Jul 02, 2002
Posts: 1865
Thank you for your question. I can see that I need to come up with something that is a little more clear. On the next version of the exam, I'll replace the old statement with this one.
"Contains no duplicate elements."

Dan Chisholm<br />SCJP 1.4<br /> <br /><a href="http://www.danchisholm.net/" target="_blank" rel="nofollow">Try my mock exam.</a>
I agree. Here's the link: http://aspose.com/file-tools
subject: Dan's questions
It's not a secret anymore!