• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 .
 
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Pavan Kumar Srinivasan, useful point, thank you, and welcome to JavaRanch
 
Rob Spoor
Sheriff
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
The happiness of your life depends upon the quality of your thoughts -Marcus Aurelius ... think about this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic