| Author |
Implementing Comparable/compareTo for a class with a collections member field
|
Salman Ahmed
Greenhorn
Joined: Mar 18, 2008
Posts: 27
|
|
Say I have a class defined as follows: and I want to implement the Comparable interface for it. Are there any standard library utilities available for comparing two collection objects and returning a (-1, 0, 1) based on how those two Collections object compare to one another? Or do I have to write my own helper/utility method to compare two Collection instances: More generally speaking, what's best practice when dealing with such classes that have to be Comparable and that contain collections member fields? Thanks.
|
 |
Rob Spoor
Sheriff
Joined: Oct 27, 2005
Posts: 19216
|
|
There are different strategies for this, but I think the most common one is this: - iterate through both collections at the same time - compare element i of collection 1 to element i of collection 2 (order determined by the collection*) - if different return that difference - loop until either collection is traversed If collection 1 still has elements return 1. If collection 2 still has elements return -1. If neither collection has elements left return 0. Now the biggest problem will be in the order (see *). If a HashSet and TreeSet contain the same elements but return those in a different order, are the collections then regarded as the same? You could solve this by using a TreeSet as intermediate solution (new TreeSet(collection1) etc) because those have an order of themselves, but is this what you want? Is the order of a List important or isn't it? Not as simple as one might think.
|
SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
|
 |
Salman Ahmed
Greenhorn
Joined: Mar 18, 2008
Posts: 27
|
|
OK, so I came up with the following utility method: but I didn't realize that different collections will return different orderings - thanks for pointing that out. The TreeSet solution works me since in my application I know that I will ALWAYS be comparing collection instances of the same type. Does this 2nd version of the code look right: Thanks Rob!
|
 |
Pavan Kumar Srinivasan
Greenhorn
Joined: Sep 17, 2008
Posts: 26
|
|
Using a Comparator over Comparable also has many advantages and can be considered a viable option 1: If the class is final or non-modifiable 2: Comparable allows us to have only one implementation ...inside the comapareTo method, We can instead have different comparator implmentations for the same class .
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32712
|
|
Pavan Kumar Srinivasan, useful point, thank you, and welcome to JavaRanch
|
 |
Rob Spoor
Sheriff
Joined: Oct 27, 2005
Posts: 19216
|
|
Both solutions look find to me. You already know that the elements in your Collections are Comparable so that's not going to be a problem with TreeSet. And see what I mean with different algorithms? Apparently, for you size is more important than the contents.
|
 |
 |
|
|
subject: Implementing Comparable/compareTo for a class with a collections member field
|
|
|