• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Rob Spoor
  • Devaka Cooray
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Jj Roberts
  • Al Hobbs
  • Piet Souris

Dan's questions

 
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
stable boy
Posts: 425
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Thomas... So this means that you cannot use a HashMap because you cannot have duplicate items, right ?
 
Ranch Hand
Posts: 168
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ok Yosi I got it... You can't add a duplicate item.
Think I misunderstood the phrasing
 
Ranch Hand
Posts: 1865
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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."
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic