[ 
https://issues.apache.org/jira/browse/COLLECTIONS-663?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16967153#comment-16967153
 ] 

Chen edited comment on COLLECTIONS-663 at 11/5/19 1:52 AM:
-----------------------------------------------------------

Hi,[~chris333]

MapIterator.setValue() really is not supported, there is a not elegant code to 
implement your problem what I understand.
{code:java}
    public void testWhatINeedToWork() {
        ListValuedMap<Integer, Integer> multiMap = new 
ArrayListValuedHashMap<>();
        multiMap.put(1, 10);
        multiMap.put(1, 20);
        multiMap.put(2, 10);
        System.out.println(multiMap);
        for(Collection<Integer> col: multiMap.asMap().values()){
            if(col instanceof  List){
                List<Integer> list = (List<Integer>) col;
                for(ListIterator it = list.listIterator(); it.hasNext(); ){
                    Integer value = (Integer) it.next();
                    if(value % 2== 0){
                        it.set(value*2);
                    }
                }
            }
        }
        System.out.println(multiMap);
    }
{code}


was (Author: guoping1):
Hi,[~chris333]

MapIterator.setValue() really is not supported, there is a not elegant code to 
implement your problem what I understand.
{code:java}
//代码占位符
    public void testWhatINeedToWork() {
        ListValuedMap<Integer, Integer> multiMap = new 
ArrayListValuedHashMap<>();
        multiMap.put(1, 10);
        multiMap.put(1, 20);
        multiMap.put(2, 10);
        System.out.println(multiMap);
        for(Collection<Integer> col: multiMap.asMap().values()){
            if(col instanceof  List){
                List<Integer> list = (List<Integer>) col;
                for(ListIterator it = list.listIterator(); it.hasNext(); ){
                    Integer value = (Integer) it.next();
                    if(value % 2== 0){
                        it.set(value*2);
                    }
                }
            }
        }
        System.out.println(multiMap);
    }
{code}

> Unexpected ConcurrentModificationException when altering Collection of a 
> MultiValuedMap
> ---------------------------------------------------------------------------------------
>
>                 Key: COLLECTIONS-663
>                 URL: https://issues.apache.org/jira/browse/COLLECTIONS-663
>             Project: Commons Collections
>          Issue Type: Bug
>            Reporter: Christophe Schmaltz
>            Assignee: Bruno P. Kinoshita
>            Priority: Trivial
>
> Testcase:
> {code}        @Test
>       public void test() {
>               MultiValuedMap<Integer, Integer> multiMap = new 
> HashSetValuedHashMap<>();
>               multiMap.put(1, 10);
>               multiMap.put(2, 20);
>               for (Collection<Integer> innerCollection : 
> multiMap.asMap().values()) {
>                       for (Iterator<Integer> iterator = 
> innerCollection.iterator(); iterator.hasNext();) {
>                               Integer i = iterator.next();
>                               iterator.remove(); // only the innerCollection 
> is altered
>                       }
>                       // innerCollection.add(6); // adding stuff back should 
> also work...
>               }
>       }{code}
> This test unexpectedly throws a ConcurrentModificationException.
> The issue is that when calling {{iterator.remove()}} the 
> {{AbstractMultiValuedMap.ValuesIterator}} detects that the Collection is 
> empty and calls {{AbstractMultiValuedMap.this.remove(key);}}.
> It may be better if the iterator of the inner collection had a reference on 
> the iterator if the outer map and called {{containerIterator.remove()}} 
> instead.
> *Note:* this solution would again present issues if the user tries to add new 
> elements in this now empty collection (which was removed from the parent).
> In the current state, it is quite unclear why an exception is thrown, without 
> debugging the code. 



--
This message was sent by Atlassian Jira
(v8.3.4#803005)

Reply via email to