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 Collections.sort 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 "Collections.sort" Watch "Collections.sort" New topic
Author

Collections.sort

Kay Osade
Greenhorn

Joined: Dec 29, 2006
Posts: 6
There's a question in the S&B test exam where ArrayList object (referenced as List) is sorted using a Collections.sort method and printed.

The sort method is invoked with a List object, not a List of Integer's or String's, but a plain old List of Object's. I thought that Object didn't implement Comparable interface and therefore the code above wouldn't compile, or at least throw an exception during the 'sorting'. But no, it compiles, runs and produces output [5, 17, 42, 812].

I guess my question is, how is it possible?

Thanks in advance
[ January 11, 2007: Message edited by: Kay Osade ]
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

If you check the API for Collections.sort, you will see that "All elements in the list must implement the Comparable interface." Integer objects do implement the Comparable interface, so they can be cast to type Comparable for sorting.

Try this...


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Kay Osade
Greenhorn

Joined: Dec 29, 2006
Posts: 6
Yes, the problems begin when a non-Comparable object is cast into one... so instead of taking a List of Comparable objects (whatever they may be - Strings, Integers, etc.), sort method casts the objects that it receives during the RT? That would explain why the compiler didn't complain about getting a List of possibly non-Comparable objects.

Thanks a lot Marc!


P.S. if anyone was wondering, all the objects in the List need to be of the same Comparable type, meaning that you can't stick a String in there, even though Strings are comparable with themselves (but not with Integers).
[ January 11, 2007: Message edited by: Kay Osade ]
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Note that if you use JDK 5+, the sort() method is defined as

This, ahhh, encourages you to use a List of some sort of Comparables. But the code shown uses a raw ArrayList type, which means that the generics info is ignored, and a compiler warning is generated.


"I'm not back." - Bill Harding, Twister
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

You can add any type of object to a (non-generic) collection, but if you try to call Collections.sort on it, then the objects need to be Comparable to avoid an exception at runtime.

This is why it's good to make all of your objects Comparable whenever possible (as well as covering some other basics like overridding toString, hashCode, equals, etc.).
[ January 11, 2007: Message edited by: marc weber ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Collections.sort
 
Similar Threads
explain the code output
ArrayList Hijinx and the S.O.P statement
Java Collections
generics wildcard
import static package