This is an automated email from the ASF dual-hosted git repository.
garydgregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-collections.git
The following commit(s) were added to refs/heads/master by this push:
new 362976168 [COLLECTIONS-889] Fix non-unique IndexedCollection remove
(#683)
362976168 is described below
commit 362976168fa5b3b066a3daa722a4629480bad267
Author: OldTruckDriver <[email protected]>
AuthorDate: Thu Jun 18 06:24:55 2026 +1000
[COLLECTIONS-889] Fix non-unique IndexedCollection remove (#683)
* [COLLECTIONS-889] Fix non-unique IndexedCollection remove
Remove only the matching key/value mapping from non-unique
IndexedCollection indexes instead of clearing the entire key bucket.
Add a regression test that removes one value while another value with the
same transformed key remains indexed.
Reviewed-by: OpenAI Codex
Reviewed-by: Anthropic Claude Code
* Clean up new test.
* Clean up changes.xml.
---------
Co-authored-by: Gary Gregory <[email protected]>
---
src/changes/changes.xml | 1 +
.../commons/collections4/collection/IndexedCollection.java | 2 +-
.../collections4/collection/IndexedCollectionTest.java | 12 ++++++++++++
3 files changed, 14 insertions(+), 1 deletion(-)
diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index f81c6f3a6..5f0b44cdd 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -50,6 +50,7 @@
<action type="fix" dev="ggregory" due-to="Dexter.k, Gary Gregory">Fix most
Junit 5 nested tests (#681).</action>
<action type="fix" dev="ggregory" due-to="Suhyeon Park, Gary Gregory"
issue="COLLECTIONS-881">MultiMapUtils.getXXX ensure safe copy (#669).</action>
<action type="fix" dev="ggregory" due-to="Dexter.k, Gary
Gregory">Re-validate entries in PredicatedMap/PredicatedCollection readObject
(#682).</action>
+ <action type="fix" dev="ggregory" due-to="Ruiqi Dong, Gary Gregory"
issue="COLLECTIONS-889">IndexedCollection.remove(Object) removes all values for
a non-unique index key.</action>
<!-- ADD -->
<action type="add" dev="ggregory" due-to="Gary Gregory">Add generics to
UnmodifiableIterator for the wrapped type.</action>
<action type="add" dev="ggregory" due-to="Gary Gregory">Add a Maven
benchmark profile for JMH.</action>
diff --git
a/src/main/java/org/apache/commons/collections4/collection/IndexedCollection.java
b/src/main/java/org/apache/commons/collections4/collection/IndexedCollection.java
index c76fd33e1..822b823e5 100644
---
a/src/main/java/org/apache/commons/collections4/collection/IndexedCollection.java
+++
b/src/main/java/org/apache/commons/collections4/collection/IndexedCollection.java
@@ -235,7 +235,7 @@ public class IndexedCollection<K, C> extends
AbstractCollectionDecorator<C> {
* @param object the object to remove
*/
private void removeFromIndex(final C object) {
- index.remove(keyTransformer.apply(object));
+ index.removeMapping(keyTransformer.apply(object), object);
}
/**
diff --git
a/src/test/java/org/apache/commons/collections4/collection/IndexedCollectionTest.java
b/src/test/java/org/apache/commons/collections4/collection/IndexedCollectionTest.java
index 94a1b8a70..db427309d 100644
---
a/src/test/java/org/apache/commons/collections4/collection/IndexedCollectionTest.java
+++
b/src/test/java/org/apache/commons/collections4/collection/IndexedCollectionTest.java
@@ -18,6 +18,7 @@ package org.apache.commons.collections4.collection;
import static java.util.Arrays.asList;
import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
@@ -154,4 +155,15 @@ class IndexedCollectionTest extends
AbstractCollectionTest<String> {
assertEquals("3", indexed.get(3));
}
+ @Test
+ void testRemovePreservesRemainingValuesWithSameTransformedKey() {
+ @SuppressWarnings("unchecked")
+ final IndexedCollection<Integer, String> indexed =
(IndexedCollection<Integer, String>) decorateCollection(new ArrayList<>());
+ indexed.add("01");
+ indexed.add("1");
+ indexed.remove("01");
+ assertEquals("1", indexed.get(1));
+ assertNotNull(indexed.values(1));
+ }
+
}