Return-Path: Delivered-To: apmail-jakarta-commons-dev-archive@apache.org Received: (qmail 93838 invoked from network); 11 Nov 2002 16:56:26 -0000 Received: from unknown (HELO nagoya.betaversion.org) (192.18.49.131) by daedalus.apache.org with SMTP; 11 Nov 2002 16:56:26 -0000 Received: (qmail 28737 invoked by uid 97); 11 Nov 2002 16:57:20 -0000 Delivered-To: qmlist-jakarta-archive-commons-dev@jakarta.apache.org Received: (qmail 28700 invoked by uid 97); 11 Nov 2002 16:57:19 -0000 Mailing-List: contact commons-dev-help@jakarta.apache.org; run by ezmlm Precedence: bulk List-Unsubscribe: List-Subscribe: List-Help: List-Post: List-Id: "Jakarta Commons Developers List" Reply-To: "Jakarta Commons Developers List" Delivered-To: mailing list commons-dev@jakarta.apache.org Received: (qmail 16974 invoked by uid 98); 11 Nov 2002 14:23:44 -0000 X-Antivirus: nagoya (v4218 created Aug 14 2002) X-Epoch: 1037024530 X-Sasl-enc: OCxCbXgIPNVXDKBNiXXmkg Message-ID: <044301c2898d$ba81fbb0$2ff1f682@har.mrc.ac.uk> From: "Rob Oxspring" To: "Jakarta Commons Developers List" References: <06DC40326E25F44682B5FDD891842303257CB4@uranium.il.kangaroo.net> <3DCED798.3080102@mindview.net> Subject: Re: [Collection] Is there a way to branch (group) a collection? Date: Mon, 11 Nov 2002 14:22:09 -0000 MIME-Version: 1.0 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: 7bit X-Priority: 3 X-MSMail-Priority: Normal X-Mailer: Microsoft Outlook Express 6.00.2800.1106 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1106 X-Spam-Rating: daedalus.apache.org 1.6.2 0/1000/N X-Spam-Rating: daedalus.apache.org 1.6.2 0/1000/N How about adding a method to CollectionUtils along the lines of the following: public static Collection group(Collection source, Transformer grouper){ Map groups = new HashMap(); Iterator i = source.iterator(); while(i.hasNext()){ Object value = i.next(); Object key = grouper.transform(value); Collection group = (Collection)groups.get(key); if(group==null){ group = new ArrayList(); groups.put(key,group); } group.add(value); } return groups.values(); } and then using a transformer along the lines of: class AgeGrouper implements Transformer{ public Object transform(Object input){ Man man = (Man)input; return String.valueOf(man.age); } } then CollectionUtils.group(source, ageGrouper) should produce something like: { {('Isac', 35) , ('Gonsales', 35)}, {('Jerry', 32) , ('Hans', 32) , ('Mussa', 32)}, {('Moshe' , 22)} } This seems to solve the required problem in a single pass, although the number of map lookups might offset this? It's a pattern I find myself repeating lots, except that I normally return the map rather than just values and I allow an optional Map parameter so that a TreeMap can be used on occasions (also allows using a non ArrayList for each group iff you know the group keys in advance). Should I send a patch? Rob ----- Original Message ----- From: "Chintan" To: "Jakarta Commons Developers List" Sent: Sunday, November 10, 2002 10:03 PM Subject: Re: [Collection] Is there a way to branch (group) a collection? > I am not aware of any direct functionality but Collection.retainAll() can be used to the grouping. > > public interface Predicate { > public boolean evaluate(Object obj); > } > > class Man { > private int age; > private String name; > private Predicate predicate; //Strategy. > > public Man(int a, String n){ > this.age = a; > this.name = n; > } > > //override equals > public boolean equals(Object obj) { > if(predicate != null) > return predicate.evaluate(obj); > else > //Override equals here. > //Do normal comparison > } > public static Collection grouped(Collection sourceCollection, > Predicate p, Man type) { > predicate = p; > return sourceCollection.retainAll(new ArrayList().add(type)); > } > } > > public class FindPredicate{ > public static void main(String[] args){ > ArrayList men = new ArrayList(); > for(int i = 0 ;i < 30; i++) > men.add(new Man("Name: " + 1, i); > //From the above collection get all men with age 25 > System.out.println(Man.grouped(men, new Predicate() { > public boolean evaluate(Object obj) { > //Compare ages. > if(age == (Man)obj.age) return true; > return false; > } > }, new Man("", 25)); > } > } > > > > Roman Rytov wrote: > > >Assume a collection of objects exists and there is an object > >implementing Predicate interface. Also let's say that evaluate(Object > >obj) method returns a number of distinct objects (out of this > >collection) and the number is noticeably less than total amount of > >objects in the collection. Is there a way to get all collections grouped > >by the predicator? An example may be schematically written like: > > > >class Man { > >String name; > >int age; > >} > > > >class AgePredicator implements Predicate { > > int age; > > boolean evaluate (Object obj) { > >Man man = (Man)obj; > >return man.age == this.age; > >} > > > > > >collection men = { ('Isac', 35), ('Jerry', 32), ('Gonsales', 35), > >('Moshe' , 22), ('Hans', 32), ('Mussa', 32)} > > > >afer grouping it's supposed to get 3 collections: > > > >('Isac', 35), ('Gonsales', 35), > >('Jerry', 32), ('Hans', 32), ('Mussa', 32)} > >('Moshe' , 22), > > > > > >Do we have something in the API do achieve it easily? > > > > > >_______________________ > >Roman Rytov > > > > > > > > > > > -- > To unsubscribe, e-mail: > For additional commands, e-mail: > > > -- To unsubscribe, e-mail: For additional commands, e-mail: