Alexander Muthmann created COLLECTIONS-529:
----------------------------------------------

             Summary: Add removeAll(Collection<E> collection, Collection<E> 
remove, Comparator<E> comparator) and contains(Collection<E> collection, E 
object, Comparator<E> comparator) methods
                 Key: COLLECTIONS-529
                 URL: https://issues.apache.org/jira/browse/COLLECTIONS-529
             Project: Commons Collections
          Issue Type: Improvement
          Components: Collection
            Reporter: Alexander Muthmann


Hi,

this request originates from one of our project where we have implemented 
something similar.

The Java-Interface java.util.Collection<E> specifies the two methods 
contains(Object o) and boolean removeAll(Collection<?> c). Both methods rely on 
the equals() method of the given Objects.

In some cases, it's not possible to change those methods and therefore 
removeAll and contains cannot be used directly. 

E.g. if you have an class myClass with property A and B and the equals method 
uses both properties, but you are only interested in property B.

To solve this problem, I'd like to propose the following extensions of 
CollectionsUtils:

{code}
/**
 * Removes all elements of remove from the collection using the comparator 
instead of .equals()
 */
public static <E> Collection<E> removeAll(final Collection<E> collection, final 
Collection<E> remove, Comparator<E> comparator);
/**
 * Checks if the given collection contains the object using the comparator 
instead of .equals()
 */
public static <E> boolean contains(final Collection<E> collection, final E 
object, Comparator<E> comparator);
{code}
Both methods do basically the same as their native equivalient but use a 
comparator instead of equals(). 

This allows the injection of any required compare value:
{code}
final Collection<myClass> result = CollectionUtils.removeAll(base, sub, new 
Comparator<myClass>() {

      public int compare(myClass o1, myClass o2) {
        return o1.getB() == o2.getB();
      }
    });
{code}



If you think those methods are a good idea (as proposed or changed according to 
any rules), please give me a short feedback and I'll offer an implementation as 
diff patch for review.

Cheers,

Alex



--
This message was sent by Atlassian JIRA
(v6.2#6252)

Reply via email to