You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by Herve Quiroz <he...@esil.univ-mrs.fr> on 2003/02/11 14:37:03 UTC

Re: [collections] filtering and splitting collections

Hi,

Well, I am not a commiter but I will give my opinion on the subject
anyway. I am not a fan of OUT parameters in method calls. So I would be in
favor of (c). First because (a) would mean API change. Also the returned
Collection could be mistaken as the good ones from the collection given in
parameter (this would not happen if everybody read carefully the API docs
anyway).

Still there is another flavor :

Collection addAll(Collection c, Predicate p)
which fills the returned collection with elements from c according to p.

This method would either be a member of a sub-interface of Collection. Or
as static method in CollectionUtils with a third parameter as the
destination collection. I know I said I don't like OUT parameters but here
it is also returned so it's not a problem IMHO.

Then if you have some sort of OppositePredicate(Predicate p) you can split
a collection in two others with two calls :

Collection s; Predicate p;

Collection greenOnes=new Collection().addAll(s, p);

Collection notSoGreen=new Collection().addAll(s, new OppositePredicate(p));

Well, I'm not sure of this one... Just my two cents.

Regards,


Herve



On Sat, 8 Feb 2003 BluePhelix@web.de wrote:

> 1. Splitting a collection
>
[...]
> I recommend to provide a method within CollectionUtils which splits a collection
> using a given predicate. The idea is to iterate through the collection only
> once. The problem is that a method with two return values can't be easily
> understood. But, in my opinion, splitting a collection is a pretty basic thing.
>
> There are three flavours to realize a collection split I would like to discuss:
> (a) changing the existing CollectionUtils.filter method to return the collection
> of rejected items
> (b) adding a CollectionUtils.filter method with a 3rd parameter for the
> collection of rejected items
> (c) explicitly introducing a method called #filterOut or #split
>
> I would agree most with solution (b):
>
>     public static void filter(Collection collection,
>                                         Predicate predicate,
>                                         Collection rejectedOutputCollection) {
>         if (collection != null && predicate != null) {
>             for (Iterator it = collection.iterator(); it.hasNext();) {
>                 Object item = it.next();
>                 if (predicate.evaluate(item) == false) {
>                     it.remove();
>                     rejectedOutputCollection.add(item);}}}}}
>
> What do you think about that?
>

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org