my dog learned polymorphism*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Collections from Dan's Exam Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Collections from Dan Watch "Collections from Dan New topic
Author

Collections from Dan's Exam

Ihab Ismail
Greenhorn

Joined: Sep 28, 2002
Posts: 14
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?!?!
Shishio San
Ranch Hand

Joined: Aug 29, 2002
Posts: 223
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.



Whatever doesn't kill us ...<br />Is probably circling back for another try.<br />SCJP 1.4
Alfred Kemety
Ranch Hand

Joined: Aug 14, 2002
Posts: 279
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?


Alfred Raouf - Egypt - SCJP 1.4<br />Kemety.equals(Egyptian) // returns true
Shishio San
Ranch Hand

Joined: Aug 29, 2002
Posts: 223
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.
Ron Newman
Ranch Hand

Joined: Jun 06, 2002
Posts: 1056
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.


Ron Newman - SCJP 1.2 (100%, 7 August 2002)
Ihab Ismail
Greenhorn

Joined: Sep 28, 2002
Posts: 14
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

Joined: Aug 29, 2002
Posts: 223
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

Joined: Jun 06, 2002
Posts: 1056
I think you're right -- both TreeSet and HashSet are correct, as are any other Sets. Dan, can you explain this answer?
Dan Chisholm
Ranch Hand

Joined: Jul 02, 2002
Posts: 1865
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.


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: Collections from Dan's Exam
 
Similar Threads
Dan's Q of Collections level
Q about Dan's Mock Exam
Dan's mock exam: collection classes
Dan's ? from Collections.
Dan's questions