Java Ninja Chronicles By Norris Shelton

Things I learned in the pursuit of code

Sorting with Java Collections is easy. Collections.sort() does all of the work if you have defined a way to compare the objects. That works great … when they are of the same type. What happens if the objects are of different types?

Create an interface that all of the objects in the collection implement. Create a comparator for the interface type. Specify the comparator to the Collection.sort() method. This is especially useful when they classes can’t have a common parent type.

I didn’t get to run an example with this type of implementation, but it makes sense. Now, if I could just remember what triggered this idea.

January 11th, 2011

Posted In: Java

Leave a Reply

Your email address will not be published. Required fields are marked *

WP to LinkedIn Auto Publish Powered By : XYZScripts.com