• Post Reply Bookmark Topic Watch Topic
  • New Topic

Implementing Comparable/compareTo for a class with a collections member field  RSS feed

 
Salman Ahmed
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 21131
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Salman Ahmed
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Posts: 56518
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pavan Kumar Srinivasan, useful point, thank you, and welcome to JavaRanch
 
Rob Spoor
Sheriff
Posts: 21131
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!