Return-Path: Delivered-To: apmail-commons-issues-archive@locus.apache.org Received: (qmail 75405 invoked from network); 2 Dec 2008 11:31:40 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (140.211.11.2) by minotaur.apache.org with SMTP; 2 Dec 2008 11:31:40 -0000 Received: (qmail 69259 invoked by uid 500); 2 Dec 2008 11:31:50 -0000 Delivered-To: apmail-commons-issues-archive@commons.apache.org Received: (qmail 68857 invoked by uid 500); 2 Dec 2008 11:31:47 -0000 Mailing-List: contact issues-help@commons.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: issues@commons.apache.org Delivered-To: mailing list issues@commons.apache.org Received: (qmail 68842 invoked by uid 99); 2 Dec 2008 11:31:47 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 02 Dec 2008 03:31:47 -0800 X-ASF-Spam-Status: No, hits=-2000.0 required=10.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.140] (HELO brutus.apache.org) (140.211.11.140) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 02 Dec 2008 11:30:27 +0000 Received: from brutus (localhost [127.0.0.1]) by brutus.apache.org (Postfix) with ESMTP id 53DEC234C298 for ; Tue, 2 Dec 2008 03:30:44 -0800 (PST) Message-ID: <1596621339.1228217444342.JavaMail.jira@brutus> Date: Tue, 2 Dec 2008 03:30:44 -0800 (PST) From: "Chris Shayan (JIRA)" To: issues@commons.apache.org Subject: [jira] Commented: (COLLECTIONS-306) Use Predicate in subtract In-Reply-To: <1930635117.1226146244614.JavaMail.jira@brutus> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 7bit X-Virus-Checked: Checked by ClamAV on apache.org [ https://issues.apache.org/jira/browse/COLLECTIONS-306?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=12652337#action_12652337 ] Chris Shayan commented on COLLECTIONS-306: ------------------------------------------ package com.chrisshayan; import org.apache.commons.collections.CollectionUtils; import org.apache.commons.collections.Predicate; import java.util.List; import java.util.ArrayList; import java.util.Collection; import java.util.Iterator; /** * Created by IntelliJ IDEA. * User: Chris Shayan and Nasehzade * Date: Nov 8, 2008 * Time: 1:59:36 PM */ public class TestSubtract { public static class Entity { private int number; public int getNumber() { return number; } public void setNumber(int number) { this.number = number; } public String toString() { return String.valueOf(getNumber()); } } /** * * @param a * @param b * @param p * @return */ public static Collection subtract(final Collection a, final Collection b, Predicate p) { ArrayList list = new ArrayList(a); for (int i = 0; i < list.size(); i++) { Object o = list.get(i); if(!p.evaluate(o)) { list.remove(i); } } return list; } public static void main(String[] args) { // simple(); List all = new ArrayList(3); Entity entity1 = new TestSubtract.Entity(); entity1.setNumber(1); Entity entity2 = new TestSubtract.Entity(); entity2.setNumber(2); Entity entity3 = new TestSubtract.Entity(); entity3.setNumber(3); all.add(entity1); all.add(entity2); all.add(entity3); final List odd = new ArrayList(); Entity entity4 = new TestSubtract.Entity(); entity4.setNumber(1); Entity entity5 = new TestSubtract.Entity(); entity5.setNumber(3); odd.add(entity4); odd.add(entity5); List all2 = new ArrayList(all); /* CollectionUtils.filter(all2, new Predicate() { public boolean evaluate(Object o) { Entity entity = (Entity) o; for (int i = 0; i < odd.size(); i++) { Entity oddEntity = (Entity) odd.get(i); if (entity.getNumber() == oddEntity.getNumber()) return false; } return true; } }); */ Collection c = subtract(all2, odd, new Predicate() { public boolean evaluate(Object o) { Entity entity = (Entity) o; for (int i = 0; i < odd.size(); i++) { Entity oddEntity = (Entity) odd.get(i); if (entity.getNumber() == oddEntity.getNumber()) return false; } return true; } }); List even = new ArrayList(c); for (int i = 0; i < even.size(); i++) { Object o = even.get(i); System.out.println("o = " + o); } } private static void simple() { List all = new ArrayList(3); all.add(Integer.valueOf("1")); all.add(Integer.valueOf("2")); all.add(Integer.valueOf("3")); List odd = new ArrayList(); odd.add(Integer.valueOf("1")); odd.add(Integer.valueOf("3")); Collection evenCollection = CollectionUtils.subtract(all, odd); List even = new ArrayList(evenCollection); for (int i = 0; i < even.size(); i++) { Object o = even.get(i); System.out.println("o = " + o); } } } > Use Predicate in subtract > ------------------------- > > Key: COLLECTIONS-306 > URL: https://issues.apache.org/jira/browse/COLLECTIONS-306 > Project: Commons Collections > Issue Type: New Feature > Components: Collection > Environment: all OSs > Reporter: Chris Shayan > Original Estimate: 0.75h > Remaining Estimate: 0.75h > > It is good idea to use Predicate in subtract method, I've developed myself the mentioned method and now I am testing it. I mean we should have following methods: > The one already exist is: > public static Collection subtract(Collection a, Collection b) > I offer to have one more which is: > public static Collection subtract(Collection a, Collection b, Predicate predicate) -- This message is automatically generated by JIRA. - You can reply to this email to add a comment to the issue online.