File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes NavigableSet Descending Order Haze Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "NavigableSet Descending Order Haze" Watch "NavigableSet Descending Order Haze" New topic
Author

NavigableSet Descending Order Haze

Gary Marshall
Ranch Hand

Joined: Feb 19, 2007
Posts: 121
All:

This code from the JavaBeat SCJP1.6 bank of questions focusing on NavigableSet:

The result of this run is "3" and I understand this result is because by the time we reach line "sysprint 2", "3" will be the last entry in the ranks TreeSet. What is confounding me is the sorting of the elements in the TreeSet. I am told that the compareTo() method in the Rank class "tries to sort the elements in descending order". Well, what displays after the execution of the "sysprint 1" line of code is:

  • 4
    5
    1
    3
    2

  • Quite possibly the answer to my question is staring me in the face and I just don't see it. Can anybody please explain to me why, given the configured compareTo() method in the Rank class, the order of the elements in the ranks treeset is not in true descending order? Why am I wrong when I am expecting the elements in the TreeSet to look like:

  • 5
    4
    3
    2
    1

  • ??
    Thank you for your time.
    Gary

    G
    Jeanne Boyarsky
    internet detective
    Marshal

    Joined: May 26, 2003
    Posts: 29219
        
    135

    The compare to method returns -1 that the first is smaller than the second. So if you say rank1.compareTo(rank2), it says rank1 is smaller. But if you say rank2.compareTo(rank1), it says rank2 is smaller. This is not a valid sort and clearly not descending order.

    This is why the order is tied to the order added. The actual values added don't matter.


    [Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
    Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
    Helen Ma
    Ranch Hand

    Joined: Nov 01, 2011
    Posts: 451
    I think:

    will sort the tree in descending order.
    This means second rank is larger than the current rank, compareTo returns a positive result. The sorting algorithm will swap these two ranks in decending order if the result is positive.
    If second rank is smaller than the current rank, compareTo returns a negative result. The sorting algorithm won't swap them.
    This will create a descending sorted order.
     
     
    subject: NavigableSet Descending Order Haze
     
    Similar Threads
    Please explain me this!
    Comparable..
    why CompareTo exists when we already have equals method
    Sorting 10 numbers
    Sorted TreeSet from HashSet