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 TreeSet should output in ascii order? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "TreeSet should output in ascii order?" Watch "TreeSet should output in ascii order?" New topic
Author

TreeSet should output in ascii order?

Nigel Shrin
Ranch Hand

Joined: May 18, 2009
Posts: 140
This is a Whizlabs example to prove the output sort sequence of a TreeSet. It outputs: "JSP Java Swing XML" - the explanation is that this is ascii value order, but its not! XML(241) and should come after JSP(237). It looks like a letter by letter ascii comparison rather than a total ascii value, is that right?

Whizlabs test program:


To prove it programmatically (because I need practice on collections):



This outputs:
LinkedHashMap output, to prove ascii ordering of TreeSet:
195: AAA
386: Java
241: XML
520: Swing
237: JSP

Second question:
The LinkedHashMap output is in key input sequence, how can I output it in key sequence, in this case by numeric magnitude?

Thank you!


Nigel
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 19073
    
  40

Nigel Shrin wrote:This is a Whizlabs example to prove the output sort sequence of a TreeSet. It outputs: "JSP Java Swing XML" - the explanation is that this is ascii value order, but its not! XML(241) and should come after JSP(237). It looks like a letter by letter ascii comparison rather than a total ascii value, is that right?


While I agree that maybe the wording could be clearer, I have to say to whizlabs defense, that no-one ever takes a sum of all the ASCII characters to determine order. Sorting by the sum of the ascii values is pretty useless, and just not done.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Nigel Shrin
Ranch Hand

Joined: May 18, 2009
Posts: 140
Thank you Henry
Prithvi Sehgal
Ranch Hand

Joined: Oct 13, 2009
Posts: 774
Hi,

Henry is absolutely correct. Great insight Henry.

Best Regards,


Prithvi,
My Blog, Follow me on Twitter,Scjp Tips, When you score low in mocks, Generics,Scjp Notes, JavaStudyGroup
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: TreeSet should output in ascii order?