[
https://issues.apache.org/jira/browse/COLLECTIONS-532?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=14013608#comment-14013608
]
Thomas Neidhart commented on COLLECTIONS-532:
---------------------------------------------
This is certainly not a bug, as the respective method clearly states what to
expect in case an invalid key/value is put into the map.
The reason an IllegalArgumentException is thrown in such a case is to be
compliant with the Map interface which states the following (put method):
{quote}
IllegalArgumentException - if some property of the specified key or value
prevents it from being stored in this map
{quote}
To filter elements from a Collection that are rejected by a Predicate one can
use CollectionUtils.filter(...), but there is no equivalent method for maps. So
this would be something useful to add to MapUtils imho.
If you want a Map that silently discards such key/value pairs I suggest to
create a custom implementation based on the existing PredicatedMap, but this
will most likely not going to be added to collections.
> MapUtils.predicatedMap(map, keypredicate,valuepredicate) is not working as
> expected
> -----------------------------------------------------------------------------------
>
> Key: COLLECTIONS-532
> URL: https://issues.apache.org/jira/browse/COLLECTIONS-532
> Project: Commons Collections
> Issue Type: Bug
> Components: Collection, Map
> Affects Versions: 4.0
> Environment: ubuntu/java8
> Reporter: Bhanupavansingh
>
> I expected that MapUtils.predicatedMap() includes those entries of the
> specified map that match the specified key-predicate and specified
> value-predicate.I expected that entries that do not match either of the
> predicates not included i the returned map.
> But in as per the code of org.apache.commons.collections4.map.
> PredicatedMap.validate()
> protected void validate(final K key, final V value) {
> if (keyPredicate != null && keyPredicate.evaluate(key) == false) {
> throw new IllegalArgumentException("Cannot add key - Predicate
> rejected it");
> }
> if (valuePredicate != null && valuePredicate.evaluate(value) ==
> false) {
> throw new IllegalArgumentException("Cannot add value - Predicate
> rejected it");
> }
> }
> if evaluation of key or value predicate fails an IllegalArgumentException is
> thrown.
> Predicates are passed to input map to test if an entry is to be included or
> not. If the evaluation of this predicate itself throws an exception based on
> result of predicate, then this method is useless and cannot be used to filter
> entries of a map(using predicates)
--
This message was sent by Atlassian JIRA
(v6.2#6252)