Reputation: 9
I am getting an exception when I try to remove an element from:
`org.eclipse.emf.common.util.EList<E> using guava Iterables.removeIf...`
The exception:
java.lang.IllegalArgumentException: The 'no duplicates' constraint is violated
at org.eclipse.emf.common.util.AbstractEList.set(AbstractEList.java:262)
at com.google.common.collect.Iterables.removeIfFromRandomAccessList(Iterables.java:198)
at com.google.common.collect.Iterables.removeIf(Iterables.java:180)
at com.mypackage.MyClass$5.applyChanges(MyClass.java:325)
The code which is giving the exception:
Iterables.removeIf(EListObj, new Predicate<Variant>()
{
@Override
public boolean apply(final Varen varen)
{
return varen.getName().equals(varenToDelete.getName()); // varenToDelete declared above in method
}
});
Upvotes: 1
Views: 552
Reputation: 38444
You didn't tell us that you're obviously using a UniqueEList
which does indeed not allow duplicate entries. From the context, it's obvious, though.
Because Guava is open-source, you can look at the code and see what's going on in there.
If you look at the implementation of the removeIf()
method, or, more concretely, at the removeIfFromRandomAccessList()
method, you'll see that the algorithm for the removing does indeed introduce short-lived duplicate items in the list. Here's how it works:
from
and one for writing to
the list, named accordingly. The from
one will get ahead while the to
will generally not make it to the end of the list.from
index (it's original position in the list) to the to
position (the position in the filtered list).Now, this implementation simply does not work for any kind of UniqueList
. I found an existing Guava bug on that issue, #1596. Feel free to comment on it / vote for it if it's still possible.
As a workaround, use Iterables.filter()
which doesn't suffer from such limitation. Mind you, it's also lazy, so it works in a different way. If you're not doing anything funky with your list, it should be fine, though.
EDIT 2015/11: This was just fixed in Guava 20 (which is not out yet).
Upvotes: 2