commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Rodney Waldhoff <rwaldh...@apache.org>
Subject Re: [collections] testing equality of collections wrt some Comparator
Date Mon, 21 Apr 2003 14:08:41 GMT
Sounds like a good application for a functional approach.  How about:

  public static boolean someNeatMethod(Iterator left, Iterator right, BinaryPredicate predicate)
{
    while(left.hasNext()) {
      if(right.hasNext()) {
        if(!predicate.test(left.next(),right.next()) {
          return false;
        }
      } else {
        // right is longer than left
        return false;
      }
    }
    if(right.hasNext()) {
      // right is longer than right
      return false;
    } else {
      return true;
    }
  }

Where in your, case you'd want:

  someNeatMethod(c1.iterator(),c2.iterator(),new IsEquivalent(new PersonComparator()));

or,

  public static someNeatMethod(Collection left, Collection right, BinaryPredicate predicate)
{
    if(left.size() != right.size()) {
      return false;
    } else {
      return someNeatMethod(left.iterator(),right.iterator(),predicate);
    }
  }

  someNeatMethod(c1,c2,new IsEquivalent(new PersonComparator()));


"someNeatMethod" might be named "pairwiseTest" or something like that, and
without modification could also be used to test that, for example, each
Person in c1 is less than the corresponding Person in c2.

You could also put this together using the existing functor algorithms and
types, for example:

  public static someNeatMethod2(Collection left, Collection right, BinaryPredicate predicate)
{
    if(left.size() != right.size()) {
      return false;
    } else {
      return null != CollectionAlgorithms.detect(
        left.iterator(),
        new UnaryPredicate() {
          public boolean test(Object obj) {
            return !(predicate.test(obj,right.next());
          }
        },
        null);
    }

but a pairwiseTest method seems like it would be useful enough to define
explicitly.

- Rod <http://radio.weblogs.com/0122027/>

On Sun, 20 Apr 2003, 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