Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
   /*
    * Copyright (C) 2007 The Guava Authors
    *
    * Licensed under the Apache License, Version 2.0 (the "License");
    * you may not use this file except in compliance with the License.
    * You may obtain a copy of the License at
    *
    * http://www.apache.org/licenses/LICENSE-2.0
    *
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS,
   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   * See the License for the specific language governing permissions and
   * limitations under the License.
   */
  
  package com.google.common.collect;
  
  import static com.google.common.base.Preconditions.checkArgument;
  import static com.google.common.base.Preconditions.checkNotNull;
  import static com.google.common.collect.CollectPreconditions.checkRemove;
  
  
  import java.util.HashSet;
  import java.util.List;
  import java.util.Map;
  import java.util.Set;
  
  import  javax.annotation.Nullable;

Provides static methods acting on or generating a Multimap.

See the Guava User Guide article on Multimaps.

Author(s):
Jared Levy
Robert Konigsberg
Mike Bostock
Louis Wasserman
Since:
2.0 (imported from Google Collections Library)
  
  @GwtCompatible(emulated = true)
  public final class Multimaps {
    private Multimaps() {}

  
Creates a new Multimap backed by map, whose internal value collections are generated by factory. Warning: do not use this method when the collections returned by factory implement either List or Set! Use the more specific method newListMultimap, newSetMultimap or newSortedSetMultimap instead, to avoid very surprising behavior from Multimap.equals.

The factory-generated and map classes determine the multimap iteration order. They also specify the behavior of the equals, hashCode, and toString methods for the multimap and its returned views. However, the multimap's get method returns instances of a different class than factory.get() does.

The multimap is serializable if map, factory, the collections generated by factory, and the multimap contents are all serializable.

The multimap is not threadsafe when any concurrent operations update the multimap, even if map and the instances generated by factory are. Concurrent read operations will work correctly. To allow concurrent update operations, wrap the multimap with a call to synchronizedMultimap.

Call this method only when the simpler methods ArrayListMultimap.create(), HashMultimap.create(), LinkedHashMultimap.create(), LinkedListMultimap.create(), TreeMultimap.create(), and TreeMultimap.create(Comparator, Comparator) won't suffice.

Note: the multimap assumes complete ownership over of map and the collections returned by factory. Those objects should not be manually updated and they should not use soft, weak, or phantom references.

Parameters:
map place to store the mapping from each key to its corresponding values
factory supplier of new, empty collections that will each hold all values for a given key
Throws:
IllegalArgumentException if map is not empty
 
   public static <K, V> Multimap<K, V> newMultimap(Map<K, Collection<V>> map,
       final Supplier<? extends Collection<V>> factory) {
     return new CustomMultimap<K, V>(mapfactory);
   }
 
   private static class CustomMultimap<K, V> extends AbstractMapBasedMultimap<K, V> {
     transient Supplier<? extends Collection<V>> factory;
 
     CustomMultimap(Map<K, Collection<V>> map,
         Supplier<? extends Collection<V>> factory) {
       super(map);
       this. = checkNotNull(factory);
     }
 
     @Override protected Collection<V> createCollection() {
       return .get();
     }
 
     // can't use Serialization writeMultimap and populateMultimap methods since
     // there's no way to generate the empty backing map.
 
    

SerialData:
the factory and the backing map
 
     @GwtIncompatible("java.io.ObjectOutputStream")
     private void writeObject(ObjectOutputStream streamthrows IOException {
       stream.defaultWriteObject();
       stream.writeObject();
       stream.writeObject(backingMap());
     }
 
     @GwtIncompatible("java.io.ObjectInputStream")
     @SuppressWarnings("unchecked"// reading data stored by writeObject
     private void readObject(ObjectInputStream stream)
         throws IOExceptionClassNotFoundException {
       stream.defaultReadObject();
        = (Supplier<? extends Collection<V>>) stream.readObject();
       Map<K, Collection<V>> map = (Map<K, Collection<V>>) stream.readObject();
       setMap(map);
     }
 
     @GwtIncompatible("java serialization not supported")
     private static final long serialVersionUID = 0;
   }

  
Creates a new ListMultimap that uses the provided map and factory. It can generate a multimap based on arbitrary Map and List classes.

The factory-generated and map classes determine the multimap iteration order. They also specify the behavior of the equals, hashCode, and toString methods for the multimap and its returned views. The multimap's get, removeAll, and replaceValues methods return RandomAccess lists if the factory does. However, the multimap's get method returns instances of a different class than does factory.get().

The multimap is serializable if map, factory, the lists generated by factory, and the multimap contents are all serializable.

The multimap is not threadsafe when any concurrent operations update the multimap, even if map and the instances generated by factory are. Concurrent read operations will work correctly. To allow concurrent update operations, wrap the multimap with a call to synchronizedListMultimap.

Call this method only when the simpler methods ArrayListMultimap.create() and LinkedListMultimap.create() won't suffice.

Note: the multimap assumes complete ownership over of map and the lists returned by factory. Those objects should not be manually updated, they should be empty when provided, and they should not use soft, weak, or phantom references.

Parameters:
map place to store the mapping from each key to its corresponding values
factory supplier of new, empty lists that will each hold all values for a given key
Throws:
IllegalArgumentException if map is not empty
 
   public static <K, V> ListMultimap<K, V> newListMultimap(
       Map<K, Collection<V>> mapfinal Supplier<? extends List<V>> factory) {
     return new CustomListMultimap<K, V>(mapfactory);
   }
 
   private static class CustomListMultimap<K, V>
       extends AbstractListMultimap<K, V> {
     transient Supplier<? extends List<V>> factory;
 
     CustomListMultimap(Map<K, Collection<V>> map,
         Supplier<? extends List<V>> factory) {
       super(map);
       this. = checkNotNull(factory);
     }
 
     @Override protected List<V> createCollection() {
       return .get();
     }

    

SerialData:
the factory and the backing map
 
     @GwtIncompatible("java.io.ObjectOutputStream")
     private void writeObject(ObjectOutputStream streamthrows IOException {
       stream.defaultWriteObject();
       stream.writeObject();
       stream.writeObject(backingMap());
     }
 
     @GwtIncompatible("java.io.ObjectInputStream")
     @SuppressWarnings("unchecked"// reading data stored by writeObject
     private void readObject(ObjectInputStream stream)
         throws IOExceptionClassNotFoundException {
       stream.defaultReadObject();
        = (Supplier<? extends List<V>>) stream.readObject();
       Map<K, Collection<V>> map = (Map<K, Collection<V>>) stream.readObject();
       setMap(map);
     }
 
     @GwtIncompatible("java serialization not supported")
     private static final long serialVersionUID = 0;
   }

  
Creates a new SetMultimap that uses the provided map and factory. It can generate a multimap based on arbitrary Map and Set classes.

The factory-generated and map classes determine the multimap iteration order. They also specify the behavior of the equals, hashCode, and toString methods for the multimap and its returned views. However, the multimap's get method returns instances of a different class than factory.get() does.

The multimap is serializable if map, factory, the sets generated by factory, and the multimap contents are all serializable.

The multimap is not threadsafe when any concurrent operations update the multimap, even if map and the instances generated by factory are. Concurrent read operations will work correctly. To allow concurrent update operations, wrap the multimap with a call to synchronizedSetMultimap.

Call this method only when the simpler methods HashMultimap.create(), LinkedHashMultimap.create(), TreeMultimap.create(), and TreeMultimap.create(Comparator, Comparator) won't suffice.

Note: the multimap assumes complete ownership over of map and the sets returned by factory. Those objects should not be manually updated and they should not use soft, weak, or phantom references.

Parameters:
map place to store the mapping from each key to its corresponding values
factory supplier of new, empty sets that will each hold all values for a given key
Throws:
IllegalArgumentException if map is not empty
 
   public static <K, V> SetMultimap<K, V> newSetMultimap(
       Map<K, Collection<V>> mapfinal Supplier<? extends Set<V>> factory) {
     return new CustomSetMultimap<K, V>(mapfactory);
   }
 
   private static class CustomSetMultimap<K, V>
       extends AbstractSetMultimap<K, V> {
     transient Supplier<? extends Set<V>> factory;
 
     CustomSetMultimap(Map<K, Collection<V>> map,
         Supplier<? extends Set<V>> factory) {
       super(map);
       this. = checkNotNull(factory);
     }
 
     @Override protected Set<V> createCollection() {
       return .get();
     }

    

SerialData:
the factory and the backing map
 
     @GwtIncompatible("java.io.ObjectOutputStream")
     private void writeObject(ObjectOutputStream streamthrows IOException {
       stream.defaultWriteObject();
       stream.writeObject();
       stream.writeObject(backingMap());
     }
 
     @GwtIncompatible("java.io.ObjectInputStream")
     @SuppressWarnings("unchecked"// reading data stored by writeObject
     private void readObject(ObjectInputStream stream)
         throws IOExceptionClassNotFoundException {
       stream.defaultReadObject();
        = (Supplier<? extends Set<V>>) stream.readObject();
       Map<K, Collection<V>> map = (Map<K, Collection<V>>) stream.readObject();
       setMap(map);
     }
 
     @GwtIncompatible("not needed in emulated source")
     private static final long serialVersionUID = 0;
   }

  
Creates a new SortedSetMultimap that uses the provided map and factory. It can generate a multimap based on arbitrary Map and SortedSet classes.

The factory-generated and map classes determine the multimap iteration order. They also specify the behavior of the equals, hashCode, and toString methods for the multimap and its returned views. However, the multimap's get method returns instances of a different class than factory.get() does.

The multimap is serializable if map, factory, the sets generated by factory, and the multimap contents are all serializable.

The multimap is not threadsafe when any concurrent operations update the multimap, even if map and the instances generated by factory are. Concurrent read operations will work correctly. To allow concurrent update operations, wrap the multimap with a call to synchronizedSortedSetMultimap.

Call this method only when the simpler methods TreeMultimap.create() and TreeMultimap.create(Comparator, Comparator) won't suffice.

Note: the multimap assumes complete ownership over of map and the sets returned by factory. Those objects should not be manually updated and they should not use soft, weak, or phantom references.

Parameters:
map place to store the mapping from each key to its corresponding values
factory supplier of new, empty sorted sets that will each hold all values for a given key
Throws:
IllegalArgumentException if map is not empty
 
   public static <K, V> SortedSetMultimap<K, V> newSortedSetMultimap(
       Map<K, Collection<V>> map,
       final Supplier<? extends SortedSet<V>> factory) {
     return new CustomSortedSetMultimap<K, V>(mapfactory);
   }
 
   private static class CustomSortedSetMultimap<K, V>
       extends AbstractSortedSetMultimap<K, V> {
     transient Supplier<? extends SortedSet<V>> factory;
     transient Comparator<? super V> valueComparator;
 
     CustomSortedSetMultimap(Map<K, Collection<V>> map,
         Supplier<? extends SortedSet<V>> factory) {
       super(map);
       this. = checkNotNull(factory);
        = factory.get().comparator();
     }
 
     @Override protected SortedSet<V> createCollection() {
       return .get();
     }
 
     @Override public Comparator<? super V> valueComparator() {
       return ;
     }

    

SerialData:
the factory and the backing map
 
     @GwtIncompatible("java.io.ObjectOutputStream")
     private void writeObject(ObjectOutputStream streamthrows IOException {
       stream.defaultWriteObject();
       stream.writeObject();
       stream.writeObject(backingMap());
     }
 
     @GwtIncompatible("java.io.ObjectInputStream")
     @SuppressWarnings("unchecked"// reading data stored by writeObject
     private void readObject(ObjectInputStream stream)
         throws IOExceptionClassNotFoundException {
       stream.defaultReadObject();
        = (Supplier<? extends SortedSet<V>>) stream.readObject();
        = .get().comparator();
       Map<K, Collection<V>> map = (Map<K, Collection<V>>) stream.readObject();
       setMap(map);
     }
 
     @GwtIncompatible("not needed in emulated source")
     private static final long serialVersionUID = 0;
   }

  
Copies each key-value mapping in source into dest, with its key and value reversed.

If source is an ImmutableMultimap, consider using ImmutableMultimap.inverse instead.

Parameters:
source any multimap
dest the multimap to copy into; usually empty
Returns:
dest
 
   public static <K, V, M extends Multimap<K, V>> M invertFrom(
       Multimap<? extends V, ? extends K> source, M dest) {
     checkNotNull(dest);
     for (Map.Entry<? extends V, ? extends K> entry : source.entries()) {
       dest.put(entry.getValue(), entry.getKey());
     }
     return dest;
   }

  
Returns a synchronized (thread-safe) multimap backed by the specified multimap. In order to guarantee serial access, it is critical that all access to the backing multimap is accomplished through the returned multimap.

It is imperative that the user manually synchronize on the returned multimap when accessing any of its collection views:

   Multimap<K, V> multimap = Multimaps.synchronizedMultimap(
       HashMultimap.<K, V>create());
   ...
   Collection<V> values = multimap.get(key);  // Needn't be in synchronized block
   ...
   synchronized (multimap) {  // Synchronizing on multimap, not values!
     Iterator<V> i = values.iterator(); // Must be in synchronized block
     while (i.hasNext()) {
       foo(i.next());
     
   }}

Failure to follow this advice may result in non-deterministic behavior.

Note that the generated multimap's Multimap.removeAll and Multimap.replaceValues methods return collections that aren't synchronized.

The returned multimap will be serializable if the specified multimap is serializable.

Parameters:
multimap the multimap to be wrapped in a synchronized view
Returns:
a synchronized view of the specified multimap
 
   public static <K, V> Multimap<K, V> synchronizedMultimap(
       Multimap<K, V> multimap) {
     return Synchronized.multimap(multimapnull);
   }

  
Returns an unmodifiable view of the specified multimap. Query operations on the returned multimap "read through" to the specified multimap, and attempts to modify the returned multimap, either directly or through the multimap's views, result in an UnsupportedOperationException.

Note that the generated multimap's Multimap.removeAll and Multimap.replaceValues methods return collections that are modifiable.

The returned multimap will be serializable if the specified multimap is serializable.

Parameters:
delegate the multimap for which an unmodifiable view is to be returned
Returns:
an unmodifiable view of the specified multimap
 
   public static <K, V> Multimap<K, V> unmodifiableMultimap(
       Multimap<K, V> delegate) {
     if (delegate instanceof UnmodifiableMultimap ||
         delegate instanceof ImmutableMultimap) {
       return delegate;
     }
     return new UnmodifiableMultimap<K, V>(delegate);
   }

  
Simply returns its argument.

Deprecated:
no need to use this
Since:
10.0
 
   @Deprecated public static <K, V> Multimap<K, V> unmodifiableMultimap(
       ImmutableMultimap<K, V> delegate) {
     return checkNotNull(delegate);
   }
 
   private static class UnmodifiableMultimap<K, V>
       extends ForwardingMultimap<K, V> implements Serializable {
     final Multimap<K, V> delegate;
     transient Collection<Entry<K, V>> entries;
     transient Multiset<K> keys;
     transient Set<K> keySet;
     transient Collection<V> values;
     transient Map<K, Collection<V>> map;
 
     UnmodifiableMultimap(final Multimap<K, V> delegate) {
       this. = checkNotNull(delegate);
     }
 
     @Override protected Multimap<K, V> delegate() {
       return ;
     }
 
     @Override public void clear() {
       throw new UnsupportedOperationException();
     }
 
     @Override public Map<K, Collection<V>> asMap() {
       Map<K, Collection<V>> result = ;
       if (result == null) {
         result =  = Collections.unmodifiableMap(
             Maps.transformValues(.asMap(), new Function<Collection<V>, Collection<V>>() {
               @Override
               public Collection<V> apply(Collection<V> collection) {
                 return unmodifiableValueCollection(collection);
               }
             }));
       }
       return result;
     }
 
     @Override public Collection<Entry<K, V>> entries() {
       Collection<Entry<K, V>> result = ;
       if (result == null) {
          = result = unmodifiableEntries(.entries());
       }
       return result;
     }
 
     @Override public Collection<V> get(K key) {
       return unmodifiableValueCollection(.get(key));
     }
 
     @Override public Multiset<K> keys() {
       Multiset<K> result = ;
       if (result == null) {
          = result = Multisets.unmodifiableMultiset(.keys());
       }
       return result;
     }
 
     @Override public Set<K> keySet() {
       Set<K> result = ;
       if (result == null) {
          = result = Collections.unmodifiableSet(.keySet());
       }
       return result;
     }
 
     @Override public boolean put(K key, V value) {
       throw new UnsupportedOperationException();
     }
 
     @Override public boolean putAll(K keyIterable<? extends V> values) {
       throw new UnsupportedOperationException();
     }
 
     @Override
     public boolean putAll(Multimap<? extends K, ? extends V> multimap) {
       throw new UnsupportedOperationException();
     }
 
     @Override public boolean remove(Object keyObject value) {
       throw new UnsupportedOperationException();
     }
 
     @Override public Collection<V> removeAll(Object key) {
       throw new UnsupportedOperationException();
     }
 
     @Override public Collection<V> replaceValues(
         K keyIterable<? extends V> values) {
       throw new UnsupportedOperationException();
     }
 
     @Override public Collection<V> values() {
       Collection<V> result = ;
       if (result == null) {
          = result = Collections.unmodifiableCollection(.values());
       }
       return result;
     }
 
     private static final long serialVersionUID = 0;
   }
 
   private static class UnmodifiableListMultimap<K, V>
       extends UnmodifiableMultimap<K, V> implements ListMultimap<K, V> {
     UnmodifiableListMultimap(ListMultimap<K, V> delegate) {
       super(delegate);
     }
     @Override public ListMultimap<K, V> delegate() {
       return (ListMultimap<K, V>) super.delegate();
     }
     @Override public List<V> get(K key) {
       return Collections.unmodifiableList(delegate().get(key));
     }
     @Override public List<V> removeAll(Object key) {
       throw new UnsupportedOperationException();
     }
     @Override public List<V> replaceValues(
         K keyIterable<? extends V> values) {
       throw new UnsupportedOperationException();
     }
     private static final long serialVersionUID = 0;
   }
 
   private static class UnmodifiableSetMultimap<K, V>
       extends UnmodifiableMultimap<K, V> implements SetMultimap<K, V> {
     UnmodifiableSetMultimap(SetMultimap<K, V> delegate) {
       super(delegate);
     }
     @Override public SetMultimap<K, V> delegate() {
       return (SetMultimap<K, V>) super.delegate();
     }
     @Override public Set<V> get(K key) {
       /*
        * Note that this doesn't return a SortedSet when delegate is a
        * SortedSetMultiset, unlike (SortedSet<V>) super.get().
        */
       return Collections.unmodifiableSet(delegate().get(key));
     }
     @Override public Set<Map.Entry<K, V>> entries() {
       return Maps.unmodifiableEntrySet(delegate().entries());
     }
     @Override public Set<V> removeAll(Object key) {
       throw new UnsupportedOperationException();
     }
     @Override public Set<V> replaceValues(
         K keyIterable<? extends V> values) {
       throw new UnsupportedOperationException();
     }
     private static final long serialVersionUID = 0;
   }
 
   private static class UnmodifiableSortedSetMultimap<K, V>
       extends UnmodifiableSetMultimap<K, V> implements SortedSetMultimap<K, V> {
       super(delegate);
     }
     @Override public SortedSetMultimap<K, V> delegate() {
       return (SortedSetMultimap<K, V>) super.delegate();
     }
     @Override public SortedSet<V> get(K key) {
       return Collections.unmodifiableSortedSet(delegate().get(key));
     }
     @Override public SortedSet<V> removeAll(Object key) {
       throw new UnsupportedOperationException();
     }
     @Override public SortedSet<V> replaceValues(
         K keyIterable<? extends V> values) {
       throw new UnsupportedOperationException();
     }
     @Override
     public Comparator<? super V> valueComparator() {
       return delegate().valueComparator();
     }
     private static final long serialVersionUID = 0;
   }

  
Returns a synchronized (thread-safe) SetMultimap backed by the specified multimap.

You must follow the warnings described in synchronizedMultimap.

The returned multimap will be serializable if the specified multimap is serializable.

Parameters:
multimap the multimap to be wrapped
Returns:
a synchronized view of the specified multimap
 
   public static <K, V> SetMultimap<K, V> synchronizedSetMultimap(
       SetMultimap<K, V> multimap) {
     return Synchronized.setMultimap(multimapnull);
   }

  
Returns an unmodifiable view of the specified SetMultimap. Query operations on the returned multimap "read through" to the specified multimap, and attempts to modify the returned multimap, either directly or through the multimap's views, result in an UnsupportedOperationException.

Note that the generated multimap's Multimap.removeAll and Multimap.replaceValues methods return collections that are modifiable.

The returned multimap will be serializable if the specified multimap is serializable.

Parameters:
delegate the multimap for which an unmodifiable view is to be returned
Returns:
an unmodifiable view of the specified multimap
 
   public static <K, V> SetMultimap<K, V> unmodifiableSetMultimap(
       SetMultimap<K, V> delegate) {
     if (delegate instanceof UnmodifiableSetMultimap ||
         delegate instanceof ImmutableSetMultimap) {
       return delegate;
     }
     return new UnmodifiableSetMultimap<K, V>(delegate);
   }

  
Simply returns its argument.

Deprecated:
no need to use this
Since:
10.0
 
   @Deprecated public static <K, V> SetMultimap<K, V> unmodifiableSetMultimap(
       ImmutableSetMultimap<K, V> delegate) {
     return checkNotNull(delegate);
   }

  
Returns a synchronized (thread-safe) SortedSetMultimap backed by the specified multimap.

You must follow the warnings described in synchronizedMultimap.

The returned multimap will be serializable if the specified multimap is serializable.

Parameters:
multimap the multimap to be wrapped
Returns:
a synchronized view of the specified multimap
 
   public static <K, V> SortedSetMultimap<K, V>
       synchronizedSortedSetMultimap(SortedSetMultimap<K, V> multimap) {
     return Synchronized.sortedSetMultimap(multimapnull);
   }

  
Returns an unmodifiable view of the specified SortedSetMultimap. Query operations on the returned multimap "read through" to the specified multimap, and attempts to modify the returned multimap, either directly or through the multimap's views, result in an UnsupportedOperationException.

Note that the generated multimap's Multimap.removeAll and Multimap.replaceValues methods return collections that are modifiable.

The returned multimap will be serializable if the specified multimap is serializable.

Parameters:
delegate the multimap for which an unmodifiable view is to be returned
Returns:
an unmodifiable view of the specified multimap
 
   public static <K, V> SortedSetMultimap<K, V> unmodifiableSortedSetMultimap(
       SortedSetMultimap<K, V> delegate) {
     if (delegate instanceof UnmodifiableSortedSetMultimap) {
       return delegate;
     }
     return new UnmodifiableSortedSetMultimap<K, V>(delegate);
   }

  
Returns a synchronized (thread-safe) ListMultimap backed by the specified multimap.

You must follow the warnings described in synchronizedMultimap.

Parameters:
multimap the multimap to be wrapped
Returns:
a synchronized view of the specified multimap
 
   public static <K, V> ListMultimap<K, V> synchronizedListMultimap(
       ListMultimap<K, V> multimap) {
     return Synchronized.listMultimap(multimapnull);
   }

  
Returns an unmodifiable view of the specified ListMultimap. Query operations on the returned multimap "read through" to the specified multimap, and attempts to modify the returned multimap, either directly or through the multimap's views, result in an UnsupportedOperationException.

Note that the generated multimap's Multimap.removeAll and Multimap.replaceValues methods return collections that are modifiable.

The returned multimap will be serializable if the specified multimap is serializable.

Parameters:
delegate the multimap for which an unmodifiable view is to be returned
Returns:
an unmodifiable view of the specified multimap
 
   public static <K, V> ListMultimap<K, V> unmodifiableListMultimap(
       ListMultimap<K, V> delegate) {
     if (delegate instanceof UnmodifiableListMultimap ||
         delegate instanceof ImmutableListMultimap) {
       return delegate;
     }
     return new UnmodifiableListMultimap<K, V>(delegate);
   }

  
Simply returns its argument.

Deprecated:
no need to use this
Since:
10.0
 
   @Deprecated public static <K, V> ListMultimap<K, V> unmodifiableListMultimap(
       ImmutableListMultimap<K, V> delegate) {
     return checkNotNull(delegate);
   }

  
Returns an unmodifiable view of the specified collection, preserving the interface for instances of SortedSet, Set, List and Collection, in that order of preference.

Parameters:
collection the collection for which to return an unmodifiable view
Returns:
an unmodifiable view of the collection
 
   private static <V> Collection<V> unmodifiableValueCollection(
       Collection<V> collection) {
     if (collection instanceof SortedSet) {
       return Collections.unmodifiableSortedSet((SortedSet<V>) collection);
     } else if (collection instanceof Set) {
       return Collections.unmodifiableSet((Set<V>) collection);
     } else if (collection instanceof List) {
       return Collections.unmodifiableList((List<V>) collection);
     }
     return Collections.unmodifiableCollection(collection);
   }

  
Returns an unmodifiable view of the specified collection of entries. The Entry.setValue operation throws an UnsupportedOperationException. If the specified collection is a Set, the returned collection is also a Set.

Parameters:
entries the entries for which to return an unmodifiable view
Returns:
an unmodifiable view of the entries
 
   private static <K, V> Collection<Entry<K, V>> unmodifiableEntries(
       Collection<Entry<K, V>> entries) {
     if (entries instanceof Set) {
       return Maps.unmodifiableEntrySet((Set<Entry<K, V>>) entries);
     }
     return new Maps.UnmodifiableEntries<K, V>(
         Collections.unmodifiableCollection(entries));
   }

  
Returns multimap.asMap(), with its type corrected from Map<K, Collection<V>> to Map<K, List<V>>.

Since:
15.0
 
   @Beta
   @SuppressWarnings("unchecked")
   // safe by specification of ListMultimap.asMap()
   public static <K, V> Map<K, List<V>> asMap(ListMultimap<K, V> multimap) {
     return (Map<K, List<V>>) (Map<K, ?>) multimap.asMap();
   }

  
Returns multimap.asMap(), with its type corrected from Map<K, Collection<V>> to Map<K, Set<V>>.

Since:
15.0
 
   @Beta
   @SuppressWarnings("unchecked")
   // safe by specification of SetMultimap.asMap()
   public static <K, V> Map<K, Set<V>> asMap(SetMultimap<K, V> multimap) {
     return (Map<K, Set<V>>) (Map<K, ?>) multimap.asMap();
   }

  
Returns multimap.asMap(), with its type corrected from Map<K, Collection<V>> to Map<K, SortedSet<V>>.

Since:
15.0
 
   @Beta
   @SuppressWarnings("unchecked")
   // safe by specification of SortedSetMultimap.asMap()
   public static <K, V> Map<K, SortedSet<V>> asMap(
       SortedSetMultimap<K, V> multimap) {
     return (Map<K, SortedSet<V>>) (Map<K, ?>) multimap.asMap();
   }

  
Returns multimap.asMap(). This is provided for parity with the other more strongly-typed asMap() implementations.

Since:
15.0
 
   @Beta
   public static <K, V> Map<K, Collection<V>> asMap(Multimap<K, V> multimap) {
     return multimap.asMap();
   }

  
Returns a multimap view of the specified map. The multimap is backed by the map, so changes to the map are reflected in the multimap, and vice versa. If the map is modified while an iteration over one of the multimap's collection views is in progress (except through the iterator's own remove operation, or through the setValue operation on a map entry returned by the iterator), the results of the iteration are undefined.

The multimap supports mapping removal, which removes the corresponding mapping from the map. It does not support any operations which might add mappings, such as put, putAll or replaceValues.

The returned multimap will be serializable if the specified map is serializable.

Parameters:
map the backing map for the returned multimap view
 
   public static <K, V> SetMultimap<K, V> forMap(Map<K, V> map) {
     return new MapMultimap<K, V>(map);
   }

  

See also:
Multimaps.forMap
 
   private static class MapMultimap<K, V>
       extends AbstractMultimap<K, V> implements SetMultimap<K, V>, Serializable {
     final Map<K, V> map;
 
     MapMultimap(Map<K, V> map) {
       this. = checkNotNull(map);
     }
 
     @Override
     public int size() {
       return .size();
     }
 
     @Override
     public boolean containsKey(Object key) {
       return .containsKey(key);
     }
 
     @Override
     public boolean containsValue(Object value) {
       return .containsValue(value);
     }
 
     @Override
     public boolean containsEntry(Object keyObject value) {
       return .entrySet().contains(Maps.immutableEntry(keyvalue));
     }
 
     @Override
     public Set<V> get(final K key) {
       return new Sets.ImprovedAbstractSet<V>() {
         @Override public Iterator<V> iterator() {
           return new Iterator<V>() {
             int i;
 
             @Override
             public boolean hasNext() {
               return ( == 0) && .containsKey(key);
             }
 
             @Override
             public V next() {
               if (!hasNext()) {
                 throw new NoSuchElementException();
               }
               ++;
               return .get(key);
             }
 
             @Override
             public void remove() {
               checkRemove( == 1);
                = -1;
               .remove(key);
             }
           };
         }
 
         @Override public int size() {
           return .containsKey(key) ? 1 : 0;
         }
       };
     }
 
     @Override
     public boolean put(K key, V value) {
       throw new UnsupportedOperationException();
     }
 
     @Override
     public boolean putAll(K keyIterable<? extends V> values) {
       throw new UnsupportedOperationException();
     }
    @Override
    public boolean putAll(Multimap<? extends K, ? extends V> multimap) {
      throw new UnsupportedOperationException();
    }
    @Override
    public Set<V> replaceValues(K keyIterable<? extends V> values) {
      throw new UnsupportedOperationException();
    }
    @Override
    public boolean remove(Object keyObject value) {
      return .entrySet().remove(Maps.immutableEntry(keyvalue));
    }
    @Override
    public Set<V> removeAll(Object key) {
      Set<V> values = new HashSet<V>(2);
      if (!.containsKey(key)) {
        return values;
      }
      values.add(.remove(key));
      return values;
    }
    @Override
    public void clear() {
      .clear();
    }
    @Override
    public Set<K> keySet() {
      return .keySet();
    }
    @Override
    public Collection<V> values() {
      return .values();
    }
    @Override
    public Set<Entry<K, V>> entries() {
      return .entrySet();
    }
    
    @Override
    Iterator<Entry<K, V>> entryIterator() {
      return .entrySet().iterator();
    }
    @Override
    Map<K, Collection<V>> createAsMap() {
      return new AsMap<K, V>(this);
    }
    @Override public int hashCode() {
      return .hashCode();
    }
    
    private static final long serialVersionUID = 7845222491160860175L;
  }

  
Returns a view of a multimap where each value is transformed by a function. All other properties of the multimap, such as iteration order, are left intact. For example, the code:
   Multimap<String, Integer> multimap =
     ImmutableSetMultimap.of("a", 2, "b", -3, "b", -3, "a", 4, "c", 6);
 Function<Integer, String> square = new Function<Integer, String>() {
     public String apply(Integer in) {
       return Integer.toString(in * in);
     
 };
 Multimap<String, String> transformed =
     Multimaps.transformValues(multimap, square);
   System.out.println(transformed);}
... prints {a=[4, 16], b=[9, 9], c=[36]}.

Changes in the underlying multimap are reflected in this view. Conversely, this view supports removal operations, and these are reflected in the underlying multimap.

It's acceptable for the underlying multimap to contain null keys, and even null values provided that the function is capable of accepting null input. The transformed multimap might contain null values, if the function sometimes gives a null result.

The returned multimap is not thread-safe or serializable, even if the underlying multimap is. The equals and hashCode methods of the returned multimap are meaningless, since there is not a definition of equals or hashCode for general collections, and get() will return a general Collection as opposed to a List or a Set.

The function is applied lazily, invoked when needed. This is necessary for the returned multimap to be a view, but it means that the function will be applied many times for bulk operations like Multimap.containsValue and Multimap.toString(). For this to perform well, function should be fast. To avoid lazy evaluation when the returned multimap doesn't need to be a view, copy the returned multimap into a new multimap of your choosing.

Since:
7.0
  public static <K, V1, V2> Multimap<K, V2> transformValues(
      Multimap<K, V1> fromMultimapfinal Function<? super V1, V2> function) {
    checkNotNull(function);
    EntryTransformer<K, V1, V2> transformer = Maps.asEntryTransformer(function);
    return transformEntries(fromMultimaptransformer);
  }

  
Returns a view of a multimap whose values are derived from the original multimap's entries. In contrast to transformValues, this method's entry-transformation logic may depend on the key as well as the value.

All other properties of the transformed multimap, such as iteration order, are left intact. For example, the code:

   SetMultimap<String, Integer> multimap =
       ImmutableSetMultimap.of("a", 1, "a", 4, "b", -6);
   EntryTransformer<String, Integer, String> transformer =
       new EntryTransformer<String, Integer, String>() {
         public String transformEntry(String key, Integer value) {
            return (value >= 0) ? key : "no" + key;
         
       };
   Multimap<String, String> transformed =
       Multimaps.transformEntries(multimap, transformer);
   System.out.println(transformed);}
... prints {a=[a, a], b=[nob]}.

Changes in the underlying multimap are reflected in this view. Conversely, this view supports removal operations, and these are reflected in the underlying multimap.

It's acceptable for the underlying multimap to contain null keys and null values provided that the transformer is capable of accepting null inputs. The transformed multimap might contain null values if the transformer sometimes gives a null result.

The returned multimap is not thread-safe or serializable, even if the underlying multimap is. The equals and hashCode methods of the returned multimap are meaningless, since there is not a definition of equals or hashCode for general collections, and get() will return a general Collection as opposed to a List or a Set.

The transformer is applied lazily, invoked when needed. This is necessary for the returned multimap to be a view, but it means that the transformer will be applied many times for bulk operations like Multimap.containsValue and Object.toString. For this to perform well, transformer should be fast. To avoid lazy evaluation when the returned multimap doesn't need to be a view, copy the returned multimap into a new multimap of your choosing.

Warning: This method assumes that for any instance k of EntryTransformer key type K, k.equals(k2) implies that k2 is also of type K. Using an EntryTransformer key type for which this may not hold, such as ArrayList, may risk a ClassCastException when calling methods on the transformed multimap.

Since:
7.0
  public static <K, V1, V2> Multimap<K, V2> transformEntries(
      Multimap<K, V1> fromMap,
      EntryTransformer<? super K, ? super V1, V2> transformer) {
    return new TransformedEntriesMultimap<K, V1, V2>(fromMaptransformer);
  }
  private static class TransformedEntriesMultimap<K, V1, V2>
      extends AbstractMultimap<K, V2> {
    final Multimap<K, V1> fromMultimap;
    final EntryTransformer<? super K, ? super V1, V2> transformer;
    TransformedEntriesMultimap(Multimap<K, V1> fromMultimap,
        final EntryTransformer<? super K, ? super V1, V2> transformer) {
      this. = checkNotNull(fromMultimap);
      this. = checkNotNull(transformer);
    }
    Collection<V2> transform(K keyCollection<V1> values) {
      Function<? super V1, V2> function = 
          Maps.asValueToValueFunction(key);
      if (values instanceof List) {
        return Lists.transform((List<V1>) valuesfunction);
      } else {
        return Collections2.transform(valuesfunction);
      }
    }
    @Override
    Map<K, Collection<V2>> createAsMap() {
      return Maps.transformEntries(.asMap(),
          new EntryTransformer<K, Collection<V1>, Collection<V2>>() {
        @Override public Collection<V2> transformEntry(
            K keyCollection<V1> value) {
          return transform(keyvalue);
        }
      });
    }
    @Override public void clear() {
      .clear();
    }
    @Override public boolean containsKey(Object key) {
      return .containsKey(key);
    }
    @Override
    Iterator<Entry<K, V2>> entryIterator() {
      return Iterators.transform(.entries().iterator(), 
          Maps.<K, V1, V2>asEntryToEntryFunction());
    }
    @Override public Collection<V2> get(final K key) {
      return transform(key.get(key));
    }
    @Override public boolean isEmpty() {
      return .isEmpty();
    }
    @Override public Set<K> keySet() {
      return .keySet();
    }
    @Override public Multiset<K> keys() {
      return .keys();
    }
    @Override public boolean put(K key, V2 value) {
      throw new UnsupportedOperationException();
    }
    @Override public boolean putAll(K keyIterable<? extends V2> values) {
      throw new UnsupportedOperationException();
    }
    @Override public boolean putAll(
        Multimap<? extends K, ? extends V2> multimap) {
      throw new UnsupportedOperationException();
    }
    @SuppressWarnings("unchecked")
    @Override public boolean remove(Object keyObject value) {
      return get((K) key).remove(value);
    }
    @SuppressWarnings("unchecked")
    @Override public Collection<V2> removeAll(Object key) {
      return transform((K) key.removeAll(key));
    }
    @Override public Collection<V2> replaceValues(
        K keyIterable<? extends V2> values) {
      throw new UnsupportedOperationException();
    }
    @Override public int size() {
      return .size();
    }
    
    @Override
    Collection<V2> createValues() {
      return Collections2.transform(
          .entries(), Maps.<K, V1, V2>asEntryToValueFunction());
    }
  }

  
Returns a view of a ListMultimap where each value is transformed by a function. All other properties of the multimap, such as iteration order, are left intact. For example, the code:
   ListMultimap<String, Integer> multimap
        = ImmutableListMultimap.of("a", 4, "a", 16, "b", 9);
   Function<Integer, Double> sqrt =
       new Function<Integer, Double>() {
         public Double apply(Integer in) {
           return Math.sqrt((int) in);
         
       };
   ListMultimap<String, Double> transformed = Multimaps.transformValues(map,
       sqrt);
   System.out.println(transformed);}
... prints {a=[2.0, 4.0], b=[3.0]}.

Changes in the underlying multimap are reflected in this view. Conversely, this view supports removal operations, and these are reflected in the underlying multimap.

It's acceptable for the underlying multimap to contain null keys, and even null values provided that the function is capable of accepting null input. The transformed multimap might contain null values, if the function sometimes gives a null result.

The returned multimap is not thread-safe or serializable, even if the underlying multimap is.

The function is applied lazily, invoked when needed. This is necessary for the returned multimap to be a view, but it means that the function will be applied many times for bulk operations like Multimap.containsValue and Multimap.toString(). For this to perform well, function should be fast. To avoid lazy evaluation when the returned multimap doesn't need to be a view, copy the returned multimap into a new multimap of your choosing.

Since:
7.0
  public static <K, V1, V2> ListMultimap<K, V2> transformValues(
      ListMultimap<K, V1> fromMultimap,
      final Function<? super V1, V2> function) {
    checkNotNull(function);
    EntryTransformer<K, V1, V2> transformer = Maps.asEntryTransformer(function);
    return transformEntries(fromMultimaptransformer);
  }

  
Returns a view of a ListMultimap whose values are derived from the original multimap's entries. In contrast to transformValues(ListMultimap, Function), this method's entry-transformation logic may depend on the key as well as the value.

All other properties of the transformed multimap, such as iteration order, are left intact. For example, the code:

   Multimap<String, Integer> multimap =
       ImmutableMultimap.of("a", 1, "a", 4, "b", 6);
   EntryTransformer<String, Integer, String> transformer =
       new EntryTransformer<String, Integer, String>() {
         public String transformEntry(String key, Integer value) {
           return key + value;
         
       };
   Multimap<String, String> transformed =
       Multimaps.transformEntries(multimap, transformer);
   System.out.println(transformed);}
... prints {"a"=["a1", "a4"], "b"=["b6"]}.

Changes in the underlying multimap are reflected in this view. Conversely, this view supports removal operations, and these are reflected in the underlying multimap.

It's acceptable for the underlying multimap to contain null keys and null values provided that the transformer is capable of accepting null inputs. The transformed multimap might contain null values if the transformer sometimes gives a null result.

The returned multimap is not thread-safe or serializable, even if the underlying multimap is.

The transformer is applied lazily, invoked when needed. This is necessary for the returned multimap to be a view, but it means that the transformer will be applied many times for bulk operations like Multimap.containsValue and Object.toString. For this to perform well, transformer should be fast. To avoid lazy evaluation when the returned multimap doesn't need to be a view, copy the returned multimap into a new multimap of your choosing.

Warning: This method assumes that for any instance k of EntryTransformer key type K, k.equals(k2) implies that k2 is also of type K. Using an EntryTransformer key type for which this may not hold, such as ArrayList, may risk a ClassCastException when calling methods on the transformed multimap.

Since:
7.0
  public static <K, V1, V2> ListMultimap<K, V2> transformEntries(
      ListMultimap<K, V1> fromMap,
      EntryTransformer<? super K, ? super V1, V2> transformer) {
    return new TransformedEntriesListMultimap<K, V1, V2>(fromMaptransformer);
  }
  private static final class TransformedEntriesListMultimap<K, V1, V2>
      extends TransformedEntriesMultimap<K, V1, V2>
      implements ListMultimap<K, V2> {
    TransformedEntriesListMultimap(ListMultimap<K, V1> fromMultimap,
        EntryTransformer<? super K, ? super V1, V2> transformer) {
      super(fromMultimaptransformer);
    }
    @Override List<V2> transform(K keyCollection<V1> values) {
      return Lists.transform((List<V1>) values, Maps.asValueToValueFunction(key));
    }
    @Override public List<V2> get(K key) {
      return transform(key.get(key));
    }
    @SuppressWarnings("unchecked")
    @Override public List<V2> removeAll(Object key) {
      return transform((K) key.removeAll(key));
    }
    @Override public List<V2> replaceValues(
        K keyIterable<? extends V2> values) {
      throw new UnsupportedOperationException();
    }
  }

  
Creates an index ImmutableListMultimap that contains the results of applying a specified function to each item in an Iterable of values. Each value will be stored as a value in the resulting multimap, yielding a multimap with the same size as the input iterable. The key used to store that value in the multimap will be the result of calling the function on that value. The resulting multimap is created as an immutable snapshot. In the returned multimap, keys appear in the order they are first encountered, and the values corresponding to each key appear in the same order as they are encountered.

For example,

   List<String> badGuys =
       Arrays.asList("Inky", "Blinky", "Pinky", "Pinky", "Clyde");
   Function<String, Integer> stringLengthFunction = ...;
   Multimap<Integer, String> index =
       Multimaps.index(badGuys, stringLengthFunction);
   System.out.println(index);

prints

   {4=[Inky], 6=[Blinky], 5=[Pinky, Pinky, Clyde]}

The returned multimap is serializable if its keys and values are all serializable.

Parameters:
values the values to use when constructing the ImmutableListMultimap
keyFunction the function used to produce the key for each value
Returns:
ImmutableListMultimap mapping the result of evaluating the function keyFunction on each value in the input collection to that value
Throws:
NullPointerException if any of the following cases is true:
  • values is null
  • keyFunction is null
  • An element in values is null
  • keyFunction returns null for any element of values
  public static <K, V> ImmutableListMultimap<K, V> index(
      Iterable<V> valuesFunction<? super V, K> keyFunction) {
    return index(values.iterator(), keyFunction);
  }

  
Creates an index ImmutableListMultimap that contains the results of applying a specified function to each item in an Iterator of values. Each value will be stored as a value in the resulting multimap, yielding a multimap with the same size as the input iterator. The key used to store that value in the multimap will be the result of calling the function on that value. The resulting multimap is created as an immutable snapshot. In the returned multimap, keys appear in the order they are first encountered, and the values corresponding to each key appear in the same order as they are encountered.

For example,

   List<String> badGuys =
       Arrays.asList("Inky", "Blinky", "Pinky", "Pinky", "Clyde");
   Function<String, Integer> stringLengthFunction = ...;
   Multimap<Integer, String> index =
       Multimaps.index(badGuys.iterator(), stringLengthFunction);
   System.out.println(index);

prints

   {4=[Inky], 6=[Blinky], 5=[Pinky, Pinky, Clyde]}

The returned multimap is serializable if its keys and values are all serializable.

Parameters:
values the values to use when constructing the ImmutableListMultimap
keyFunction the function used to produce the key for each value
Returns:
ImmutableListMultimap mapping the result of evaluating the function keyFunction on each value in the input collection to that value
Throws:
NullPointerException if any of the following cases is true:
  • values is null
  • keyFunction is null
  • An element in values is null
  • keyFunction returns null for any element of values
Since:
10.0
  public static <K, V> ImmutableListMultimap<K, V> index(
      Iterator<V> valuesFunction<? super V, K> keyFunction) {
    checkNotNull(keyFunction);
    ImmutableListMultimap.Builder<K, V> builder
        = ImmutableListMultimap.builder();
    while (values.hasNext()) {
      V value = values.next();
      checkNotNull(valuevalues);
      builder.put(keyFunction.apply(value), value);
    }
    return builder.build();
  }
  static class Keys<K, V> extends AbstractMultiset<K> {
    final Multimap<K, V> multimap;
    
    Keys(Multimap<K, V> multimap) {
      this. = multimap;
    }
      return new TransformedIterator<Map.Entry<K, Collection<V>>, Multiset.Entry<K>>(
          .asMap().entrySet().iterator()) {
        @Override
        Multiset.Entry<K> transform(
            final Map.Entry<K, Collection<V>> backingEntry) {
          return new Multisets.AbstractEntry<K>() {
            @Override
            public K getElement() {
              return backingEntry.getKey();
            }
            @Override
            public int getCount() {
              return backingEntry.getValue().size();
            }
          };
        }
      };
    }
    @Override int distinctElements() {
      return .asMap().size();
    }
      return new KeysEntrySet();
    }
    class KeysEntrySet extends Multisets.EntrySet<K> {
      @Override Multiset<K> multiset() {
        return Keys.this;
      }
      @Override public Iterator<Multiset.Entry<K>> iterator() {
        return entryIterator();
      }
      @Override public int size() {
        return distinctElements();
      }
      @Override public boolean isEmpty() {
        return .isEmpty();
      }
      @Override public boolean contains(@Nullable Object o) {
        if (o instanceof Multiset.Entry) {
          Multiset.Entry<?> entry = (Multiset.Entry<?>) o;
          Collection<V> collection = .asMap().get(entry.getElement());
          return collection != null && collection.size() == entry.getCount();
        }
        return false;
      }
      @Override public boolean remove(@Nullable Object o) {
        if (o instanceof Multiset.Entry) {
          Multiset.Entry<?> entry = (Multiset.Entry<?>) o;
          Collection<V> collection = .asMap().get(entry.getElement());
          if (collection != null && collection.size() == entry.getCount()) {
            collection.clear();
            return true;
          }
        }
        return false;
      }