public static <K extends Serializable, V, C extends Collection<V> & Serializable> boolean removeValueFromMultiValuedCache( Cache<K, C> cache, K key, V element )
    {
        final Lock lock = cache.getLock( key );
        lock.lock();
        try
        {
            final C elements = cache.get( key );

            if ( elements == null ) {
                return false;
            }

            // Remove all instances of the element from the entry value.
            boolean changed = false;
            while ( elements.remove( element ) )
            {
                changed = true;
            }

            if ( changed )
            {
                if ( elements.isEmpty() )
                {
                    // When after removal, the value is empty, remove the cache entry completely.
                    cache.remove( key );
                }
                else
                {
                    // The cluster-based cache needs an explicit 'put' to cause the change to propagate.
                    cache.put( key, elements );
                }
            }
            return changed;
        }
        finally
        {
            lock.unlock();
        }
    }
Removes all instances of the specified element from the collection that is mapped by the provided key.

When the element removed from the set leaves that set empty, the cache entry is removed completely.

The implementation of this method is designed to be compatible with both clustered as well as non-clustered caches.

@param cache   The cache from which to remove the element (cannot be null).
@param key     The cache entry identifier (cannot be null)
@param <K> the type of key contained by the cache
@param <V> the type of value contained by the cache
@param <C> the type of collection contained by the cache
@param element The element to be removed (can be null only if the value-Collection supports null values).
@return True if the collection was modified
xmppserver/src/main/java/org/jivesoftware/util/cache/CacheUtil.java