• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Collections from Dan's Exam

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Collections Question from Dan's Exam:
-----------------------------
a. Each element must be unique.
b. Duplicate elements must not replace old
elements.
c. Elements are not key/value pairs.

Which of these classes provides the specified features?
a. LinkedList
b. TreeMap
c. TreeSet
d. HashMap
e. HashSet
f. Hashtable
g. All of the above
h. None of the above
--------------------------------------------
The answer is e. HashSet
I don't understand why c.TreeSet is not also correct! Any idea?
The answer explains that TreeSet sorts the elements according to the Key! I also dont understand how that is so? I thought keys were used in List implementations not Set. I also don't know why the sorting issue is relevant to the question?!?!
 
Ranch Hand
Posts: 223
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I think it's because TreeSet is backed by a TreeMap instance


public class TreeSet
extends AbstractSet
implements SortedSet, Cloneable, Serializable
This class implements the Set interface, backed by a TreeMap instance. This class guarantees that the sorted set will be in ascending element order, sorted according to the natural order of the elements (see Comparable), or by the comparator provided at set creation time, depending on which constructor is used.


 
Ranch Hand
Posts: 279
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
well what I don't understand is, how come elements must be unique and Duplicates must not replace the existing elements, how would it work then? it won't allow inserting the new one?
 
Shishio San
Ranch Hand
Posts: 223
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Alfred Kemety:
well what I don't understand is, how come elements must be unique and Duplicates must not replace the existing elements, how would it work then? it won't allow inserting the new one?


It means that when you try to add a new element that already exists the add method will return false and the collection (set) won't be modified.
 
Ranch Hand
Posts: 1056
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A TreeSet differs from a HashSet precisely because it maintains an ordering.
An element can be in any Set no more than once. Trying to put the same element in the Set again would be a no-op.
 
Ihab Ismail
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry but I still don't get it:
---------------------------------------
I think it's because TreeSet is backed by a TreeMap instance
---------------------------------------
So which of the three features in the question does TreeSet NOT satisfy
----------------------------------------
A TreeSet differs from a HashSet precisely because it maintains an ordering.
----------------------------------------
And how does that make it not qualify for a correct answer for the above question?
 
Shishio San
Ranch Hand
Posts: 223
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Ihab Ismail:
[QB]Sorry but I still don't get it:
---------------------------------------
I think it's because TreeSet is backed by a TreeMap instance
---------------------------------------
So which of the three features in the question does TreeSet NOT satisfy


I guess C
 
Ron Newman
Ranch Hand
Posts: 1056
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think you're right -- both TreeSet and HashSet are correct, as are any other Sets. Dan, can you explain this answer?
 
Ranch Hand
Posts: 1865
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Ron Newman:
I think you're right -- both TreeSet and HashSet are correct, as are any other Sets. Dan, can you explain this answer?


Yes I can explain. My explanation quoted in the first post of this thread states that the TreeSet was eliminated because the elements are sorted. The problem with the question was that I left out the requirement for elements that are not sorted. I just uploaded a correction as follows.

I'm sorry about the confusion.
 
Screaming fools! It's nothing more than a tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic