Return-Path: X-Original-To: apmail-commons-issues-archive@minotaur.apache.org Delivered-To: apmail-commons-issues-archive@minotaur.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 951B117AEB for ; Mon, 12 Jan 2015 06:04:33 +0000 (UTC) Received: (qmail 44696 invoked by uid 500); 12 Jan 2015 06:04:35 -0000 Delivered-To: apmail-commons-issues-archive@commons.apache.org Received: (qmail 44604 invoked by uid 500); 12 Jan 2015 06:04:34 -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 44593 invoked by uid 99); 12 Jan 2015 06:04:34 -0000 Received: from arcas.apache.org (HELO arcas.apache.org) (140.211.11.28) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 12 Jan 2015 06:04:34 +0000 Date: Mon, 12 Jan 2015 06:04:34 +0000 (UTC) From: "Oswaldo Olivo (JIRA)" To: issues@commons.apache.org Message-ID: In-Reply-To: References: Subject: [jira] [Created] (COLLECTIONS-545) Undocumented performance issue in the removeAll method in CollectionUtils MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 7bit X-JIRA-FingerPrint: 30527f35849b9dde25b450d4833f0394 Oswaldo Olivo created COLLECTIONS-545: ----------------------------------------- Summary: Undocumented performance issue in the removeAll method in CollectionUtils Key: COLLECTIONS-545 URL: https://issues.apache.org/jira/browse/COLLECTIONS-545 Project: Commons Collections Issue Type: Bug Components: Collection Affects Versions: 4.1 Environment: Ubuntu 14.04 Reporter: Oswaldo Olivo Priority: Trivial This bug is analogous to https://issues.apache.org/jira/browse/COLLECTIONS-544 The method removeAll in CollectionUtils is inefficient when the second parameter collection has a slow containment method. The following is the current implementation with its documentation: ============================ /** * Removes the elements in remove from collection. That is, this * method returns a collection containing all the elements in c * that are not in remove. The cardinality of an element e * in the returned collection is the same as the cardinality of e * in collection unless remove contains e, in which * case the cardinality is zero. This method is useful if you do not wish to modify * the collection c and thus cannot call collection.removeAll(remove);. * * @param the type of object the {@link Collection} contains * @param collection the collection from which items are removed (in the returned collection) * @param remove the items to be removed from the returned collection * @return a Collection containing all the elements of collection except * any elements that also occur in remove. * @throws NullPointerException if either parameter is null * @since 4.0 (method existed in 3.2 but was completely broken) */ public static Collection removeAll(final Collection collection, final Collection remove) { return ListUtils.removeAll(collection, remove); } ======================================= We can notice the inefficiency by looking at the removeAll method in ListUtils. The removeAll method from ListUtils is implemented and documented as follows: ======================================= /** * Removes the elements in remove from collection. That is, this * method returns a list containing all the elements in collection * that are not in remove. The cardinality of an element e * in the returned collection is the same as the cardinality of e * in collection unless remove contains e, in which * case the cardinality is zero. This method is useful if you do not wish to modify * collection and thus cannot call collection.removeAll(remove);. *

* This implementation iterates over collection, checking each element in * turn to see if it's contained in remove. If it's not contained, it's added * to the returned list. As a consequence, it is advised to use a collection type for * remove that provides a fast (e.g. O(1)) implementation of * {@link Collection#contains(Object)}. * * @param the element type * @param collection the collection from which items are removed (in the returned collection) * @param remove the items to be removed from the returned collection * @return a List containing all the elements of c except * any elements that also occur in remove. * @throws NullPointerException if either parameter is null * @since 3.2 */ public static List removeAll(final Collection collection, final Collection remove) { final List list = new ArrayList(); for (final E obj : collection) { if (!remove.contains(obj)) { list.add(obj); } } return list; } ======================================= In the case of ListUtils:removeAll, the inefficiency is properly documented. Perhaps the disclaimer about potential inefficiencies depending on the type of the parameter collection in ListUtils:removeAll should also be included in CollectionUtils:removeAll. -- This message was sent by Atlassian JIRA (v6.3.4#6332)