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 java.util.Map;
 import java.util.Set;

Implementation of Multimap using hash tables.

The multimap does not store duplicate key-value pairs. Adding a new key-value pair equal to an existing key-value pair has no effect.

Keys and values may be null. All optional multimap methods are supported, and all returned views are modifiable.

This class is not threadsafe when any concurrent operations update the multimap. Concurrent read operations will work correctly. To allow concurrent update operations, wrap your multimap with a call to Multimaps.synchronizedSetMultimap(com.google.common.collect.SetMultimap).

Author(s):
Jared Levy
Since:
2.0 (imported from Google Collections Library)
 
 @GwtCompatible(serializable = true, emulated = true)
 public final class HashMultimap<K, V> extends AbstractSetMultimap<K, V> {
   private static final int DEFAULT_VALUES_PER_KEY = 2;
 
   transient int expectedValuesPerKey = ;

  
Creates a new, empty HashMultimap with the default initial capacities.
 
   public static <K, V> HashMultimap<K, V> create() {
     return new HashMultimap<K, V>();
   }

  
Constructs an empty HashMultimap with enough capacity to hold the specified numbers of keys and values without rehashing.

Parameters:
expectedKeys the expected number of distinct keys
expectedValuesPerKey the expected average number of values per key
Throws:
java.lang.IllegalArgumentException if expectedKeys or expectedValuesPerKey is negative
 
   public static <K, V> HashMultimap<K, V> create(
       int expectedKeysint expectedValuesPerKey) {
     return new HashMultimap<K, V>(expectedKeysexpectedValuesPerKey);
   }

  
Constructs a HashMultimap with the same mappings as the specified multimap. If a key-value mapping appears multiple times in the input multimap, it only appears once in the constructed multimap.

Parameters:
multimap the multimap whose contents are copied to this multimap
 
   public static <K, V> HashMultimap<K, V> create(
       Multimap<? extends K, ? extends V> multimap) {
     return new HashMultimap<K, V>(multimap);
   }
 
   private HashMultimap() {
     super(new HashMap<K, Collection<V>>());
   }
 
   private HashMultimap(int expectedKeysint expectedValuesPerKey) {
     super(Maps.<K, Collection<V>>newHashMapWithExpectedSize(expectedKeys));
     Preconditions.checkArgument(expectedValuesPerKey >= 0);
     this. = expectedValuesPerKey;
   }
 
  private HashMultimap(Multimap<? extends K, ? extends V> multimap) {
    super(Maps.<K, Collection<V>>newHashMapWithExpectedSize(
        multimap.keySet().size()));
    putAll(multimap);
  }

  

Creates an empty HashSet for a collection of values for one key.

Returns:
a new HashSet containing a collection of values for one key
  }

  

SerialData:
expectedValuesPerKey, number of distinct keys, and then for each distinct key: the key, number of values for that key, and the key's values
  @GwtIncompatible("java.io.ObjectOutputStream")
  private void writeObject(ObjectOutputStream streamthrows IOException {
    stream.defaultWriteObject();
    Serialization.writeMultimap(thisstream);
  }
  @GwtIncompatible("java.io.ObjectInputStream")
  private void readObject(ObjectInputStream stream)
      throws IOExceptionClassNotFoundException {
    stream.defaultReadObject();
     = stream.readInt();
    int distinctKeys = Serialization.readCount(stream);
    Map<K, Collection<V>> map = Maps.newHashMapWithExpectedSize(distinctKeys);
    setMap(map);
    Serialization.populateMultimap(thisstreamdistinctKeys);
  }
  @GwtIncompatible("Not needed in emulated source")
  private static final long serialVersionUID = 0;
New to GrepCode? Check out our FAQ X