Return-Path: Delivered-To: apmail-jakarta-commons-dev-archive@apache.org Received: (qmail 4183 invoked from network); 10 Nov 2002 22:36:04 -0000 Received: from unknown (HELO nagoya.betaversion.org) (192.18.49.131) by daedalus.apache.org with SMTP; 10 Nov 2002 22:36:04 -0000 Received: (qmail 22920 invoked by uid 97); 10 Nov 2002 22:37:03 -0000 Delivered-To: qmlist-jakarta-archive-commons-dev@jakarta.apache.org Received: (qmail 22902 invoked by uid 97); 10 Nov 2002 22:37:03 -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 22890 invoked by uid 98); 10 Nov 2002 22:37:02 -0000 X-Antivirus: nagoya (v4218 created Aug 14 2002) X-MimeOLE: Produced By Microsoft Exchange V6.0.5762.3 content-class: urn:content-classes:message MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: quoted-printable Subject: RE: [Collection] Is there a way to branch (group) a collection? Date: Mon, 11 Nov 2002 00:36:02 +0200 Message-ID: <06DC40326E25F44682B5FDD8918423034A54EE@uranium.il.kangaroo.net> X-MS-Has-Attach: X-MS-TNEF-Correlator: Thread-Topic: [Collection] Is there a way to branch (group) a collection? Thread-Index: AcKJBSoAHhRozeYQQyCXamFBYv1u0QABFAhQ From: "Roman Rytov" To: "Jakarta Commons Developers List" X-Spam-Rating: daedalus.apache.org 1.6.2 0/1000/N X-Spam-Rating: daedalus.apache.org 1.6.2 0/1000/N It's not exactly what I suggested. The problem of this approach is it's up to a caller to chose which element the collection should retained of. My example has a practical ancestor. A databse returns a collection of objects possessing a type-attribute. I have to treat them separetely (grouped by this attribute). There is an obvious way to group the collection on one pass without enumerating all kinds of the attribute's values. Otherwise either your approach or by help of FilterIterator we have to go through the collection as many as a distinct nubmer of the type attribute.=20 > -----Original Message----- > From: Chintan [mailto:chintan@mindview.net]=20 > Sent: Monday, November 11, 2002 12:03 AM > To: Jakarta Commons Developers List > Subject: Re: [Collection] Is there a way to branch (group) a=20 > collection? >=20 >=20 > I am not aware of any direct functionality but=20 > Collection.retainAll() can be used to the grouping. >=20 > public interface Predicate { > public boolean evaluate(Object obj); > } >=20 > class Man { > private int age; > private String name; > private Predicate predicate; //Strategy. >=20 > public Man(int a, String n){ > this.age =3D a; > this.name =3D n; > } >=20 > //override equals > public boolean equals(Object obj) { > if(predicate !=3D null) > return predicate.evaluate(obj); > else=20 > //Override equals here. > //Do normal comparison=20 > } > public static Collection grouped(Collection sourceCollection,=20 > Predicate p, Man type) { > predicate =3D p; > return sourceCollection.retainAll(new ArrayList().add(type)); > } > } >=20 > public class FindPredicate{ > public static void main(String[] args){ > ArrayList men =3D new ArrayList(); > for(int i =3D 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 =3D=3D (Man)obj.age) return true; > return false; > } > }, new Man("", 25)); > } > } >=20 >=20 >=20 > Roman Rytov wrote: >=20 > >Assume a collection of objects exists and there is an object=20 > >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=20 > >objects in the collection. Is there a way to get all collections=20 > >grouped by the predicator? An example may be schematically written=20 > >like: > >=20 > >class Man { > >String name; > >int age; > >} > >=20 > >class AgePredicator implements Predicate { > > int age; > > boolean evaluate (Object obj) { > >Man man =3D (Man)obj; > >return man.age =3D=3D this.age; > >} > >=20 > >=20 > >collection men =3D { ('Isac', 35), ('Jerry', 32), ('Gonsales', 35),=20 > >('Moshe' , 22), ('Hans', 32), ('Mussa', 32)} > >=20 > >afer grouping it's supposed to get 3 collections: > >=20 > >('Isac', 35), ('Gonsales', 35), > >('Jerry', 32), ('Hans', 32), ('Mussa', 32)} > >('Moshe' , 22),=20 > >=20 > >=20 > >Do we have something in the API do achieve it easily? > >=20 > >=20 > >_______________________ > >Roman Rytov > > > > > > =20 > > >=20 >=20 > -- > To unsubscribe, e-mail: =20 > unsubscribe@jakarta.apache.org> > For=20 > additional commands,=20 > e-mail: >=20 >=20 -- To unsubscribe, e-mail: For additional commands, e-mail: