You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@commons.apache.org by Kishore Senji <ks...@gmail.com> on 2005/01/14 20:02:15 UTC

Re: [Collections] Searching and filtering collections with Functors, Predicates, etc

Here is a simple example

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.Predicate;
import org.apache.commons.lang.builder.ToStringBuilder;

import java.util.Collection;
import java.util.ArrayList;

public class TestCollections {

	private static final Collection projects = new ArrayList();


	public static void main(String[] args){

		/* for your second question; to find something in a collection */
		System.out.println(CollectionUtils.find(projects, new Predicate(){
			public boolean evaluate (Object o){
				return o instanceof Project ? ((Project)o).getId() == 2: false;
			}
		}));

		/* for your fist question; filter the collection */
		CollectionUtils.filter(projects, new Predicate(){
			public boolean evaluate (Object o){
				return o instanceof Project ? ((Project)o).getId() % 2 == 0: false;
			}
		});

		System.out.println(projects);
	}

	static {
		projects.add(new Project(1, "Jakarta Commons - Collections"));
		projects.add(new Project(2, "Jakarta Commons - Collections"));
		projects.add(new Project(3, "Jakarta Commons - Attributes"));
		projects.add(new Project(4, "Jakarta Commons - BeanUtils"));
		projects.add(new Project(5, "Jakarta Commons - Betwixt"));
		projects.add(new Project(6, "Jakarta Commons - Chain"));
		projects.add(new Project(7, "Jakarta Commons - CLI"));
		projects.add(new Project(8, "Jakarta Commons - Codec"));
		projects.add(new Project(9, "Jakarta Commons - Collections"));
		projects.add(new Project(10, "Jakarta Commons - Configuration"));
		projects.add(new Project(11, "Jakarta Commons - Daemon"));
		projects.add(new Project(12, "Jakarta Commons - DBCP"));
		projects.add(new Project(13, "Jakarta Commons - DbUtils"));
		projects.add(new Project(14, "Jakarta Commons - Digester"));
		projects.add(new Project(15, "Jakarta Commons - Discovery"));
		projects.add(new Project(16, "Jakarta Commons - EL"));
		projects.add(new Project(17, "Jakarta Commons - Email"));
		projects.add(new Project(18, "Jakarta Commons - FileUpload"));
		projects.add(new Project(19, "Jakarta Commons - HttpClient"));
		projects.add(new Project(20, "Jakarta Commons - IO"));
		projects.add(new Project(21, "Jakarta Commons - Jelly"));
		projects.add(new Project(22, "Jakarta Commons - Jexl"));
		projects.add(new Project(23, "Jakarta Commons - JXPath"));
		projects.add(new Project(24, "Jakarta Commons - Lang"));
		projects.add(new Project(25, "Jakarta Commons - Latka"));
		projects.add(new Project(26, "Jakarta Commons - Launcher"));
		projects.add(new Project(27, "Jakarta Commons - Logging"));
		projects.add(new Project(28, "Jakarta Commons - Math"));
		projects.add(new Project(29, "Jakarta Commons - Modeler"));
		projects.add(new Project(30, "Jakarta Commons - Net"));
		projects.add(new Project(31, "Jakarta Commons - Pool"));
		projects.add(new Project(32, "Jakarta Commons - Primitives"));
		projects.add(new Project(33, "Jakarta Commons - Resources"));
		projects.add(new Project(34, "Jakarta Commons - Transaction"));
		projects.add(new Project(35, "Jakarta Commons - Validator"));

	}

	private static class Project {

		private int id;
		private String name;

		public Project(int id, String name) {
			this.id = id;
			this.name = name;
		}

		public int getId(){
			return this.id;
		}

		public String getName(){
			return this.name;
		}

		public String toString(){
			return ToStringBuilder.reflectionToString(this);
		}
	}
}


On Fri, 14 Jan 2005 14:52:22 -0200, Leandro Rodrigo Saad Cruz
<le...@ibnetwork.com.br> wrote:
> Hi all. Can you give an example on how I could achieve this
> functionality with commons code :
> 
> - Add/Remove an object to a collection : traverse the collection,
> perform and arbitrary test on its elements, then add/remove the object
> according to the results (e.g. no element with the same ID found)
> 
> - Return an object from collection : traverse the collection, perform
> and arbitrary test on its elements, then return the object according to
> the results (e.g. if and element with a given ID is present)
> 
> Thanks !
> --
> Leandro Rodrigo Saad Cruz
> CTO / InterBusiness Tecnologia e Serviços
> OJB - db.apache.org/ojb
> XINGU - xingu.sf.net
> SITE DA FESTA - www.sitedafesta.com.br
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: commons-user-help@jakarta.apache.org
> 
>

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