commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From scolebou...@btopenworld.com
Subject Re: [collections] testing equality of collections wrt some Comparator
Date Sun, 20 Apr 2003 16:53:36 GMT
What worries me most about this question is the desire to not put equals/hashcode methods on
the Person class. Without these, the class will not perform correctly in any collection.

That said, the idea of being able to compare collections for equality using some measure other
than the default definition might be a useful addition to [collections]. Perhaps rather than
using a Comparator it might use a Predicate to check if each pair of elements is equal. However,
there is still the question of determining which pair to compare. For a List it is easy, but
otherwise...

So [collections] does not support this at present, but if a suitable generic enough method
can be written, I might be prepared to see one added.

Stephen

>  from:    _matthewHawthorne <mhawthorne@alumni.pitt.edu>
> 
> This is a tough question.
> 
> I believe that Comparators are generally used to compare individual
> items inside of a Collection, to find the min and max, not to check for
> equality.  So, in looking around the java.util and
> org.apache.commons.collections packages, I couldn't find any method that
> take a Comparator and 2 Collections.
> 
> So, in short, I don't think that SomeClass.someNeatMethod exists.
> 
> You are correct that using the equals() method would be a quick fix to
> your problem.  Otherwise, it seems that you'll have to create a method
> that takes 2 Collections and a Comparator (or 2 Objects and a
> Comparator) and returns a boolean.
> 
> Sorry I couldn't be of more help, maybe there is some piece of the API
> that I'm missing.
> 
> 
> 
> 
> On Sun, 2003-04-20 at 09:31, Mischa Barthel wrote:
> > Hi,
> > 
> > Suppose I have got two Collections, col1 and col2 of some elements. Now
> > I also have a Comparator, ElementComparator, that compares these
> > element. Now I want to be able to check whether or not the two
> > collections are equal and I want this to be the case iff these
> > collections contain the same elements with the same cardinality, where
> > elements are the same, when they are equal according to the
> > ElementComparator.
> > 
> > I can implement this myself of course, but I was wondering if there are
> > already methods implemented, that do this. (I could not find them). An
> > example of the situation above:
> > 
> > public class Person {
> >   private int _id;
> >   private String name;
> >   private String address;
> > 
> >   // getters and setters for all properties above
> >   ...
> > }
> > 
> > public class PersonComparator implements Comparator {
> >   public int compare(Object obj1, Object obj2) {
> >     Person p1 = (Person) obj1;
> >     Person p2 = (Person) obj2;
> >     
> >     // Compare without taking _id into account.
> >     // The ordering relation is not important for this example.
> >     // The point is that equality is reached whenever
> >     // names and addresses match.
> >     int nameCompare = p1.getName().compareTo(p2.getName());
> >     if (nameCompare == 0) {
> >       return p1.getAddress().compareTo(p2.getAddress());
> >     } else {
> >       return nameCompare;
> >     }
> >   }
> > }
> > 
> > Now I want the collections c1 = [(1, john, a street), (2, alice, another
> > street)] and c2 = [(11, john, a street), (12, alice, another street)] to
> > be equal with respect to the PersonComparator. And I want to be able to
> > check this with something like:
> > 
> > boolean equalCollections = SomeClass.someNeatMethod(c1, c2,
> > PersonComparator);
> > 
> > So my question is: Does SomeClass.someNeatMethod exist?
> > 
> > I could also solve this by implementing Person.equals() and then using
> > Vector.equals(). But I don't want (id, name, address) to be equal to
> > (anotherId, name, address) in general. Hence the PersonComparator.
> > 
> > Greetings,
> > 
> > Mischa.
> > 
> > 
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
> > For additional commands, e-mail: commons-user-help@jakarta.apache.org
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: commons-user-help@jakarta.apache.org
> 


Mime
View raw message