Unfortunately the iterator retrieved from the stream cannot support the remove operation. This is due to the fact that an entry in the cache can map to possibly more than 1 value.
There are a few alternatives:
- If you are iterating all values, you can just use the iterator on the keySet, entrySet or values. This will work unless you have a CacheStore in your cache. I logged [ISPN-5804] Cache collections iterator doesn't support remove with a cache loader - JBoss Issue Tracker to fix that.
- You can just directly invoke Cache.remove of the key of the value (under the scenes this is all the iterator used to do).
- Preferred method: You can utilize the new distributed streams to run the removal local to the node where it is owned. This can be done by the following:
cache.keySet().stream() .filter((Serializable & Predicate<Map.Entry<Integer, ?>>) e -> e.getKey() > 10) .forEach((Serializable & Consumer<Integer>) k -> GetCacheAfterSerialization.get().remove(k));
The above code would given a cache where the key is an Integer remove all entries that have a key that is greater than 10. Note this task is ran on each node and is applied local to all data, which should provide substantial performance improvement over iterating over the entries and invoking remove remotely. Unfortunately the above code you have to obtain a reference to your cache manually (I made up a GetCacheAfterSerialization.get() call to do this). We have plans of allowing injection of a Cache after the Consumer provided forEach is deserialized as well: [ISPN-5805] Allow forEach consumer to have a Cache Injected - JBoss Issue Tracker