commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From _matthewHawthorne <mhawtho...@alumni.pitt.edu>
Subject Re: [collections] testing equality of collections wrt some Comparator
Date Sun, 20 Apr 2003 14:54:54 GMT
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


Mime
View raw message