commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Sergey Yevtushenko <ser...@inferenzsysteme.informatik.tu-darmstadt.de>
Subject [collections] suggestion for change in MultiHasMap
Date Sat, 19 Oct 2002 08:04:15 GMT
Current implementation of  MultiHashMap has one problem with notion of 
equality.
Namely, when for some key some value is added, and then removed from 
MultiMap, and this value was only one value, that was put for this key
than multimap wouldn't be equal to with same values of key/value pairs, 
in which this new value was not added.

This can be demonstrated by following test (can be added to 
TestMultiHashMap.testMapEquals):

 public void testMapEquals() {
        MultiHashMap one = new MultiHashMap();
        Integer value = new Integer(1);
        one.put("One", value);
        one.remove("One", value);

        MultiHashMap two = new MultiHashMap();
        assertEquals(two, one);
    }

Suggested fix for this problem is the following change in code of 
MultiHashMap.remove(Object key, Object value).

public Object remove( Object key, Object item )
    {
        ArrayList valuesForKey = (ArrayList) super.get( key );
       
        if ( valuesForKey == null )
            return null;
       
      valuesForKey.remove( item );
//start of change
        if(valuesForKey.isEmpty()){
            remove(key);
        }
//end of change
        return item;
    }

Best Regards.

Sergey Yevtushenko



--
To unsubscribe, e-mail:   <mailto:commons-dev-unsubscribe@jakarta.apache.org>
For additional commands, e-mail: <mailto:commons-dev-help@jakarta.apache.org>


Mime
View raw message