You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by Sergey Yevtushenko <se...@inferenzsysteme.informatik.tu-darmstadt.de> on 2002/10/19 10:04:15 UTC
[collections] suggestion for change in MultiHasMap
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: <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>
Re: [collections] suggestion for change in MultiHasMap
Posted by "Michael A. Smith" <ma...@apache.org>.
We're in the release process so this cannot be fixed at the moment, so
please file a bug in Bugzilla so we don't lose track of this:
http://issues.apache.org/bugzilla/enter_bug.cgi?product=Commons
regards,
michael
Sergey Yevtushenko wrote:
> 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:
> <ma...@jakarta.apache.org>
> For additional commands, e-mail:
> <ma...@jakarta.apache.org>
--
To unsubscribe, e-mail: <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>