guava/src/com/google/common/collect/Multimaps.java
public static <K, V> ListMultimap<K, V> filterKeys(
      ListMultimap<K, V> unfiltered, final Predicate<? super K> keyPredicate) {
    if (unfiltered instanceof FilteredKeyListMultimap) {
      FilteredKeyListMultimap<K, V> prev = (FilteredKeyListMultimap<K, V>) unfiltered;
      return new FilteredKeyListMultimap<>(
          prev.unfiltered(), Predicates.<K>and(prev.keyPredicate, keyPredicate));
    } else {
      return new FilteredKeyListMultimap<>(unfiltered, keyPredicate);
    }
  }
public static <K, V> ListMultimap<K, V> filterKeys(
      ListMultimap<K, V> unfiltered, final Predicate<? super K> keyPredicate) {
    if (unfiltered instanceof FilteredKeyListMultimap) {
      FilteredKeyListMultimap<K, V> prev = (FilteredKeyListMultimap<K, V>) unfiltered;
      return new FilteredKeyListMultimap<>(
          prev.unfiltered(), Predicates.<K>and(prev.keyPredicate, keyPredicate));
    } else {
      return new FilteredKeyListMultimap<>(unfiltered, keyPredicate);
    }
  }
Returns a multimap containing the mappings in {@code unfiltered} whose keys satisfy a
predicate. The returned multimap is a live view of {@code unfiltered}; changes to one affect
the other.

<p>The resulting multimap's views have iterators that don't support {@code remove()}, but all
other methods are supported by the multimap and its views. When adding a key that doesn't
satisfy the predicate, the multimap's {@code put()}, {@code putAll()}, and {@code
replaceValues()} methods throw an {@link IllegalArgumentException}.

<p>When methods such as {@code removeAll()} and {@code clear()} are called on the filtered
multimap or its views, only mappings whose keys satisfy the filter will be removed from the
underlying multimap.

<p>The returned multimap isn't threadsafe or serializable, even if {@code unfiltered} is.

<p>Many of the filtered multimap's methods, such as {@code size()}, iterate across every
key/value mapping in the underlying multimap and determine which satisfy the filter. When a
live view is <i>not</i> needed, it may be faster to copy the filtered multimap and use the
copy.

<p><b>Warning:</b> {@code keyPredicate} must be <i>consistent with equals</i>, as documented at
{@link Predicate#apply}. Do not provide a predicate such as {@code
Predicates.instanceOf(ArrayList.class)}, which is inconsistent with equals.

@since 14.0