2011-12-04 18 views
26

CollectionUtils::removeAll() Commons Collections 3.2.1जावा कॉमन्स संग्रह removeAll

मैं, पागल हो रहा किया जाना चाहिए, क्योंकि यह इस पद्धति की तरह लगता है क्या डॉक्स राज्य का उल्टा कर रही है:

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.

इस छोटे JUnit परीक्षण

@Test 
public void testCommonsRemoveAll() throws Exception { 
    String str1 = "foo"; 
    String str2 = "bar"; 
    String str3 = "qux"; 

    List<String> collection = Arrays.asList(str1, str2, str3); 
    System.out.println("collection: " + collection); 

    List<String> remove = Arrays.asList(str1); 
    System.out.println("remove: " + remove); 

    Collection result = CollectionUtils.removeAll(collection, remove); 
    System.out.println("result: " + result); 
    assertEquals(2, result.size()); 
} 

java.lang.AssertionError: expected:<2> but was:<1>

के साथ असफल रहा है

और प्रिंट

collection: [foo, bar, qux] 
remove: [foo] 
result: [foo] 
डॉक्स मैं [bar, qux] की उम्मीद करनी चाहिए की मेरी पढ़ने से

। मुझे क्या याद आया?

+0

मैंने अपनी पोस्ट को इस पर प्रतिबिंबित करने के लिए अपडेट किया क्योंकि किसी ने मुझे इसकी याद दिला दी - लेकिन इस मुद्दे के लिए एक उपाय के साथ अपाचे कॉमन्स कलेक्शन 4.0 नवंबर 2013 में जारी किया गया था। – birryree

उत्तर

34

1 जनवरी, 2014 संपादित करें अपाचे कॉमन्स संग्रह 4.0 अंततः 21 नवंबर, 2013 को जारी किया गया था, और इस मुद्दे के लिए एक फिक्स शामिल है। प्रश्न (1688 - 1691) में

Link to CollectionUtils.java

लाइन्स, विधि पावती के साथ पहले से टूट गया था:

/* 
... 
* @since 4.0 (method existed in 3.2 but was completely broken) 
*/ 
public static <E> Collection<E> removeAll(final Collection<E> collection, final Collection<?> remove) { 
    return ListUtils.removeAll(collection, remove); 
} 

मूल उत्तर

नहीं, आप नहीं कर रहे हैं पागल। removeAll() वास्तव में (गलत तरीके से) retainAll() पर कॉल कर रहा है।

यह CollectionUtils में एक बग है, जो संस्करण 3.2 को प्रभावित करता है। यह तय किया गया है, लेकिन केवल 4.0 शाखा में।

https://issues.apache.org/jira/browse/COLLECTIONS-349

और के रूप में एक और सबूत है, यहाँ स्रोत कोड के लिए एक लिंक है:

http://svn.apache.org/repos/asf/commons/proper/collections/tags/COLLECTIONS_3_2/src/java/org/apache/commons/collections/CollectionUtils.java

इस लाइन चेक आउट:

public static Collection removeAll(Collection collection, Collection remove) { 
    return ListUtils.retainAll(collection, remove); 
} 

हाँ ... टूट!

+1

पवित्र धूम्रपान करता है! यह दरारों के माध्यम से कैसे पर्ची? जानकारी के लिए धन्यवाद। ऊपर उठो और आपके लिए स्वीकार करें। – markdsievers

+0

@markdsievers - ऐसा लगता है कि यूनिट परीक्षण की आवश्यकता है, या फिक्सिंग की आवश्यकता है! – birryree

+0

आईएमओ, यह बहुत खराब है। गलतियां ठीक हैं, लेकिन मूल मुद्दे में "02/Aug/06 17:37" का सृजन स्टैंप है, और उन्होंने अभी भी इसमें फिक्स के साथ उत्पादन रिलीज़ नहीं किया है। –

संबंधित मुद्दे