You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@commons.apache.org by "Thomas Neidhart (JIRA)" <ji...@apache.org> on 2015/11/27 22:12:13 UTC

[jira] [Closed] (COLLECTIONS-529) Add removeAll(Collection collection, Collection remove, Comparator comparator) and contains(Collection collection, E object, Comparator comparator) methods

     [ https://issues.apache.org/jira/browse/COLLECTIONS-529?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]

Thomas Neidhart closed COLLECTIONS-529.
---------------------------------------

> 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
>             Fix For: 4.1
>
>         Attachments: COLLECTIONS-529.patch, COLLECTIONS-529_1.patch
>
>
> 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.3.4#6332)