This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Sets Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Sets" Watch "Sets" New topic


Neil Muya
Ranch Hand

Joined: Oct 26, 2009
Posts: 30
Hi ranchers this is a code from ExamLab just edited a part to make it work.

I know that :
negative if objOne < objTwo
zero if objOne == objTwo
positive if objOne > objTwo

Here's my question:

how does the set arrange the elements?

Because as of now what I just know is:
if you compare the "1st argument to the 2nd argument" in compare method you get an natural order, or you get a duplicate
if you compare the "2nd argument to the 1st argument" in compare method you get an reverse order , or you get a duplicate

In this method does the set insert the 1st element I added comparing to the second element?

and if it gets a "1"?it puts "ob1" to cs[0]?

sorry I am kinda confused.

Thanks in advance!
Ralph Jaus
Ranch Hand

Joined: Apr 27, 2008
Posts: 342
Hi Neil,

to simplify writing I set a1 = new A(1) and respectively a2, a3.

Now, when cs.add(a1) is performed, the objects a1 and a3 are compared. Actually it doesn't matter if compare(a1,a3) or
compare(a3,a1) is inoked, since in any case the comparison yields a1 < a3. For:

compare(a1,a3) yields -1, meaning a1 < a3.
compare(a3,a1) yields +1, meaning a3 > a1, which is equivalent to a1 < a3.

So after adding a1, the order of cs is {a1,a3}.

When adding a2, the three elements a1, a2, a3 are compared, yielding the order {a1,a2,a3}.

Hope it helps.

SCJP 5 (98%) - SCBCD 5 (98%)
I agree. Here's the link:
subject: Sets