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 14375bdba Fix Flat3Map entrySet remove ignoring the entry value (#714)
14375bdba is described below
commit 14375bdba38421c174d646c40b8b757cce52dd45
Author: Naveed Khan <[email protected]>
AuthorDate: Fri Jul 17 14:40:35 2026 +0000
Fix Flat3Map entrySet remove ignoring the entry value (#714)
* fix Flat3Map entrySet remove ignoring the entry value
EntrySet.remove deleted on a key match alone; guard with contains(obj) so
only a full key-and-value match removes, matching AbstractHashedMap.
* Update Flat3MapTest.java
* Update changes.xml
---------
Co-authored-by: Gary Gregory <[email protected]>
---
src/changes/changes.xml | 3 ++-
.../org/apache/commons/collections4/map/Flat3Map.java | 9 +++++----
.../apache/commons/collections4/map/Flat3MapTest.java | 16 ++++++++++++++++
3 files changed, 23 insertions(+), 5 deletions(-)
diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index 5ec0369b1..840b76a1f 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -24,7 +24,6 @@
<body>
<release version="4.6.0" date="YYYY-MM-DD" description="This is a feature
and maintenance release. Java 8 or later is required.">
<!-- FIX -->
- <action type="fix" dev="ggregory" due-to="Naveed Khan">SetUniqueList and
ListOrderedSet leave the backing collection inconsistent when add(int, Object)
or addAll(int, Collection) is called with an out-of-range index.</action>
<action type="fix" dev="ggregory" due-to="Naveed Khan, Gary
Gregory">DualHashBidiMap, DualLinkedHashBidiMap, and DualTreeBidiMap entrySet()
Map.Entry.setValue(Object) returns the new value instead of the previous
value.</action>
<action type="fix" dev="ggregory" due-to="Naveed Khan, Gary
Gregory">AbstractMapBag and AbstractMapMultiSet.add(Object, int) overflow the
size and element count past Integer.MAX_VALUE.</action>
<action type="fix" dev="ggregory" due-to="Naveed
Khan">CompositeCollection, CompositeSet, CompositeMap, AbstractMultiValuedMap
and AbstractMultiSet size() overflow int when the total exceeds
Integer.MAX_VALUE.</action>
@@ -83,6 +82,8 @@
<action type="fix" dev="ggregory" due-to="Naveed Khan, Gary Gregory">Clamp
size() int overflow in composite and multi-valued classes (#710).</action>
<action type="fix" dev="ggregory" due-to="Naveed Khan, Gary Gregory">Fix
AbstractDualBidiMap.MapEntry.setValue returning new value (#711).</action>
<action type="fix" dev="ggregory" due-to="sankalp suman, Gary Gregory"
issue="COLLECTIONS-714">PatriciaTrie ignores trailing null characters in keys
(#712).</action>
+ <action type="fix" dev="ggregory" due-to="Naveed Khan, Gary
Gregory">Flat3Map entrySet().remove(Object) removes a mapping when only the key
matches, ignoring the entry value.</action>
+ <action type="fix" dev="ggregory" due-to="Naveed Khan, Gary
Gregory">SetUniqueList and ListOrderedSet leave the backing collection
inconsistent when add(int, Object) or addAll(int, Collection) is called with an
out-of-range index.</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/map/Flat3Map.java
b/src/main/java/org/apache/commons/collections4/map/Flat3Map.java
index ed7fd90c2..45b07fb13 100644
--- a/src/main/java/org/apache/commons/collections4/map/Flat3Map.java
+++ b/src/main/java/org/apache/commons/collections4/map/Flat3Map.java
@@ -149,11 +149,12 @@ public class Flat3Map<K, V> implements IterableMap<K, V>,
Serializable, Cloneabl
if (!(obj instanceof Map.Entry)) {
return false;
}
+ if (!contains(obj)) {
+ return false;
+ }
final Map.Entry<?, ?> entry = (Map.Entry<?, ?>) obj;
- final Object key = entry.getKey();
- final boolean result = parent.containsKey(key);
- parent.remove(key);
- return result;
+ parent.remove(entry.getKey());
+ return true;
}
@Override
diff --git
a/src/test/java/org/apache/commons/collections4/map/Flat3MapTest.java
b/src/test/java/org/apache/commons/collections4/map/Flat3MapTest.java
index 3cd0b58f2..31080f92d 100644
--- a/src/test/java/org/apache/commons/collections4/map/Flat3MapTest.java
+++ b/src/test/java/org/apache/commons/collections4/map/Flat3MapTest.java
@@ -24,6 +24,7 @@ import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertSame;
import static org.junit.jupiter.api.Assertions.assertTrue;
+import java.util.AbstractMap;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
@@ -277,6 +278,21 @@ public class Flat3MapTest<K, V> extends
AbstractIterableMapTest<K, V> {
assertTrue(contains);
}
+ @Test
+ void testEntrySetRemoveChecksValue() {
+ final Flat3Map<Integer, String> m = new Flat3Map<>();
+ m.put(ONE, TEN);
+ m.put(TWO, TWENTY);
+ // key present but value differs: entrySet().remove must not remove
+ assertFalse(m.entrySet().remove(new AbstractMap.SimpleEntry<>(ONE,
TWENTY)));
+ assertEquals(2, m.size());
+ assertEquals(TEN, m.get(ONE));
+ // matching key and value: removes
+ assertTrue(m.entrySet().remove(new AbstractMap.SimpleEntry<>(ONE,
TEN)));
+ assertFalse(m.containsKey(ONE));
+ assertEquals(1, m.size());
+ }
+
@Test
@SuppressWarnings("unchecked")
void testEntryIteratorSetValue1() throws Exception {