Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Dan's questions

 
Kristof Camelbeke
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 425
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
Dan?
 
Kristof Camelbeke
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Thomas... So this means that you cannot use a HashMap because you cannot have duplicate items, right ?
 
Yosi Hendarsjah
Ranch Hand
Posts: 166
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 97
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok Yosi I got it... You can't add a duplicate item.
Think I misunderstood the phrasing
 
Dan Chisholm
Ranch Hand
Posts: 1865
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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."
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic