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 ]
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 ]
Joined: Nov 28, 2001
ok Yosi I got it... You can't add a duplicate item. Think I misunderstood the phrasing
Kristof, 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>