From 84a2460ffee5bbd6f93802f53c967930886fac3f Mon Sep 17 00:00:00 2001 From: data17 Date: Wed, 6 Nov 2019 15:10:46 +0800 Subject: [PATCH] [COLLECTIONS-663] Update AbstractMultiValuedMap asMap() --- .../multimap/AbstractMultiValuedMap.java | 3 +- .../multimap/AbstractMultiValuedMapTest.java | 43 +++++++++++++++++++ 2 files changed, 45 insertions(+), 1 deletion(-) diff --git a/src/main/java/org/apache/commons/collections4/multimap/AbstractMultiValuedMap.java b/src/main/java/org/apache/commons/collections4/multimap/AbstractMultiValuedMap.java index 2beda5ae69..c0f2f72136 100644 --- a/src/main/java/org/apache/commons/collections4/multimap/AbstractMultiValuedMap.java +++ b/src/main/java/org/apache/commons/collections4/multimap/AbstractMultiValuedMap.java @@ -888,7 +888,8 @@ class AsMapEntrySetIterator extends AbstractIteratorDecorator> next() { final Map.Entry> entry = super.next(); final K key = entry.getKey(); - return new UnmodifiableMapEntry<>(key, wrappedCollection(key)); + final Collection value = entry.getValue(); + return new UnmodifiableMapEntry<>(key, value); } } } diff --git a/src/test/java/org/apache/commons/collections4/multimap/AbstractMultiValuedMapTest.java b/src/test/java/org/apache/commons/collections4/multimap/AbstractMultiValuedMapTest.java index ea851cc924..f2d2a635e7 100644 --- a/src/test/java/org/apache/commons/collections4/multimap/AbstractMultiValuedMapTest.java +++ b/src/test/java/org/apache/commons/collections4/multimap/AbstractMultiValuedMapTest.java @@ -750,6 +750,49 @@ public void testAsMapRemove() { assertEquals(4, getMap().size()); } + public void testAsMapValues() { + if(!isAddSupported() || isHashSetValue()){ + return; + } + final MultiValuedMap multiMap = makeObject(); + multiMap.put((K) "A", (V) "W"); + multiMap.put((K) "A", (V) "X"); + multiMap.put((K) "A", (V) "F"); + multiMap.put((K) "B", (V) "Q"); + multiMap.put((K) "B", (V) "Q"); + multiMap.put((K) "B", (V) "L"); + assertEquals("{A=[W, X, F], B=[Q, Q, L]}", multiMap.toString()); + for(Collection list : multiMap.asMap().values()){ + for(Iterator it = list.iterator(); it.hasNext(); ){ + V i = it.next(); + it.remove(); + } + } + assertEquals("{A=[], B=[]}", multiMap.toString()); + } + + public void testAsMapEntrySet() { + if(!isAddSupported() || isHashSetValue()){ + return; + } + final MultiValuedMap multiMap = makeObject(); + multiMap.put((K) "A", (V) "W"); + multiMap.put((K) "A", (V) "X"); + multiMap.put((K) "A", (V) "F"); + multiMap.put((K) "B", (V) "Q"); + multiMap.put((K) "B", (V) "Q"); + multiMap.put((K) "B", (V) "L"); + assertEquals("{A=[W, X, F], B=[Q, Q, L]}", multiMap.toString()); + Map> asMap = multiMap.asMap(); + for(Map.Entry> entry : asMap.entrySet()){ + for(Iterator it = entry.getValue().iterator(); it.hasNext(); ){ + V str = it.next(); + it.remove(); + } + } + assertEquals("{A=[], B=[]}", multiMap.toString()); + } + public void testMapIterator() { resetEmpty(); MapIterator mapIt = getMap().mapIterator();