001/*
002 * Copyright (C) 2007 The Guava Authors
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package com.google.common.collect;
018
019import static com.google.common.base.Preconditions.checkPositionIndex;
020import static com.google.common.base.Preconditions.checkState;
021import static com.google.common.collect.CollectPreconditions.checkRemove;
022import static java.util.Collections.unmodifiableList;
023
024import com.google.common.annotations.GwtCompatible;
025import com.google.common.annotations.GwtIncompatible;
026import com.google.j2objc.annotations.WeakOuter;
027
028import java.io.IOException;
029import java.io.ObjectInputStream;
030import java.io.ObjectOutputStream;
031import java.io.Serializable;
032import java.util.AbstractSequentialList;
033import java.util.Collection;
034import java.util.ConcurrentModificationException;
035import java.util.HashMap;
036import java.util.Iterator;
037import java.util.List;
038import java.util.ListIterator;
039import java.util.Map;
040import java.util.Map.Entry;
041import java.util.NoSuchElementException;
042import java.util.Set;
043
044import javax.annotation.Nullable;
045
046/**
047 * An implementation of {@code ListMultimap} that supports deterministic
048 * iteration order for both keys and values. The iteration order is preserved
049 * across non-distinct key values. For example, for the following multimap
050 * definition: <pre>   {@code
051 *
052 *   Multimap<K, V> multimap = LinkedListMultimap.create();
053 *   multimap.put(key1, foo);
054 *   multimap.put(key2, bar);
055 *   multimap.put(key1, baz);}</pre>
056 *
057 * ... the iteration order for {@link #keys()} is {@code [key1, key2, key1]},
058 * and similarly for {@link #entries()}. Unlike {@link LinkedHashMultimap}, the
059 * iteration order is kept consistent between keys, entries and values. For
060 * example, calling: <pre>   {@code
061 *
062 *   map.remove(key1, foo);}</pre>
063 *
064 * <p>changes the entries iteration order to {@code [key2=bar, key1=baz]} and the
065 * key iteration order to {@code [key2, key1]}. The {@link #entries()} iterator
066 * returns mutable map entries, and {@link #replaceValues} attempts to preserve
067 * iteration order as much as possible.
068 *
069 * <p>The collections returned by {@link #keySet()} and {@link #asMap} iterate
070 * through the keys in the order they were first added to the multimap.
071 * Similarly, {@link #get}, {@link #removeAll}, and {@link #replaceValues}
072 * return collections that iterate through the values in the order they were
073 * added. The collections generated by {@link #entries()}, {@link #keys()}, and
074 * {@link #values} iterate across the key-value mappings in the order they were
075 * added to the multimap.
076 *
077 * <p>The {@link #values()} and {@link #entries()} methods both return a
078 * {@code List}, instead of the {@code Collection} specified by the {@link
079 * ListMultimap} interface.
080 *
081 * <p>The methods {@link #get}, {@link #keySet()}, {@link #keys()},
082 * {@link #values}, {@link #entries()}, and {@link #asMap} return collections
083 * that are views of the multimap. If the multimap is modified while an
084 * iteration over any of those collections is in progress, except through the
085 * iterator's methods, the results of the iteration are undefined.
086 *
087 * <p>Keys and values may be null. All optional multimap methods are supported,
088 * and all returned views are modifiable.
089 *
090 * <p>This class is not threadsafe when any concurrent operations update the
091 * multimap. Concurrent read operations will work correctly. To allow concurrent
092 * update operations, wrap your multimap with a call to {@link
093 * Multimaps#synchronizedListMultimap}.
094 *
095 * <p>See the Guava User Guide article on <a href=
096 * "https://github.com/google/guava/wiki/NewCollectionTypesExplained#multimap">
097 * {@code Multimap}</a>.
098 *
099 * @author Mike Bostock
100 * @since 2.0
101 */
102@GwtCompatible(serializable = true, emulated = true)
103public class LinkedListMultimap<K, V> extends AbstractMultimap<K, V>
104    implements ListMultimap<K, V>, Serializable {
105  /*
106   * Order is maintained using a linked list containing all key-value pairs. In
107   * addition, a series of disjoint linked lists of "siblings", each containing
108   * the values for a specific key, is used to implement {@link
109   * ValueForKeyIterator} in constant time.
110   */
111
112  private static final class Node<K, V> extends AbstractMapEntry<K, V> {
113    final K key;
114    V value;
115    Node<K, V> next; // the next node (with any key)
116    Node<K, V> previous; // the previous node (with any key)
117    Node<K, V> nextSibling; // the next node with the same key
118    Node<K, V> previousSibling; // the previous node with the same key
119
120    Node(@Nullable K key, @Nullable V value) {
121      this.key = key;
122      this.value = value;
123    }
124
125    @Override
126    public K getKey() {
127      return key;
128    }
129
130    @Override
131    public V getValue() {
132      return value;
133    }
134
135    @Override
136    public V setValue(@Nullable V newValue) {
137      V result = value;
138      this.value = newValue;
139      return result;
140    }
141  }
142
143  private static class KeyList<K, V> {
144    Node<K, V> head;
145    Node<K, V> tail;
146    int count;
147
148    KeyList(Node<K, V> firstNode) {
149      this.head = firstNode;
150      this.tail = firstNode;
151      firstNode.previousSibling = null;
152      firstNode.nextSibling = null;
153      this.count = 1;
154    }
155  }
156
157  private transient Node<K, V> head; // the head for all keys
158  private transient Node<K, V> tail; // the tail for all keys
159  private transient Map<K, KeyList<K, V>> keyToKeyList;
160  private transient int size;
161
162  /*
163   * Tracks modifications to keyToKeyList so that addition or removal of keys invalidates
164   * preexisting iterators. This does *not* track simple additions and removals of values
165   * that are not the first to be added or last to be removed for their key.
166   */
167  private transient int modCount;
168
169  /**
170   * Creates a new, empty {@code LinkedListMultimap} with the default initial
171   * capacity.
172   */
173  public static <K, V> LinkedListMultimap<K, V> create() {
174    return new LinkedListMultimap<K, V>();
175  }
176
177  /**
178   * Constructs an empty {@code LinkedListMultimap} with enough capacity to hold
179   * the specified number of keys without rehashing.
180   *
181   * @param expectedKeys the expected number of distinct keys
182   * @throws IllegalArgumentException if {@code expectedKeys} is negative
183   */
184  public static <K, V> LinkedListMultimap<K, V> create(int expectedKeys) {
185    return new LinkedListMultimap<K, V>(expectedKeys);
186  }
187
188  /**
189   * Constructs a {@code LinkedListMultimap} with the same mappings as the
190   * specified {@code Multimap}. The new multimap has the same
191   * {@link Multimap#entries()} iteration order as the input multimap.
192   *
193   * @param multimap the multimap whose contents are copied to this multimap
194   */
195  public static <K, V> LinkedListMultimap<K, V> create(
196      Multimap<? extends K, ? extends V> multimap) {
197    return new LinkedListMultimap<K, V>(multimap);
198  }
199
200  LinkedListMultimap() {
201    keyToKeyList = Maps.newHashMap();
202  }
203
204  private LinkedListMultimap(int expectedKeys) {
205    keyToKeyList = new HashMap<K, KeyList<K, V>>(expectedKeys);
206  }
207
208  private LinkedListMultimap(Multimap<? extends K, ? extends V> multimap) {
209    this(multimap.keySet().size());
210    putAll(multimap);
211  }
212
213  /**
214   * Adds a new node for the specified key-value pair before the specified
215   * {@code nextSibling} element, or at the end of the list if {@code
216   * nextSibling} is null. Note: if {@code nextSibling} is specified, it MUST be
217   * for an node for the same {@code key}!
218   */
219  private Node<K, V> addNode(@Nullable K key, @Nullable V value, @Nullable Node<K, V> nextSibling) {
220    Node<K, V> node = new Node<K, V>(key, value);
221    if (head == null) { // empty list
222      head = tail = node;
223      keyToKeyList.put(key, new KeyList<K, V>(node));
224      modCount++;
225    } else if (nextSibling == null) { // non-empty list, add to tail
226      tail.next = node;
227      node.previous = tail;
228      tail = node;
229      KeyList<K, V> keyList = keyToKeyList.get(key);
230      if (keyList == null) {
231        keyToKeyList.put(key, keyList = new KeyList<K, V>(node));
232        modCount++;
233      } else {
234        keyList.count++;
235        Node<K, V> keyTail = keyList.tail;
236        keyTail.nextSibling = node;
237        node.previousSibling = keyTail;
238        keyList.tail = node;
239      }
240    } else { // non-empty list, insert before nextSibling
241      KeyList<K, V> keyList = keyToKeyList.get(key);
242      keyList.count++;
243      node.previous = nextSibling.previous;
244      node.previousSibling = nextSibling.previousSibling;
245      node.next = nextSibling;
246      node.nextSibling = nextSibling;
247      if (nextSibling.previousSibling == null) { // nextSibling was key head
248        keyToKeyList.get(key).head = node;
249      } else {
250        nextSibling.previousSibling.nextSibling = node;
251      }
252      if (nextSibling.previous == null) { // nextSibling was head
253        head = node;
254      } else {
255        nextSibling.previous.next = node;
256      }
257      nextSibling.previous = node;
258      nextSibling.previousSibling = node;
259    }
260    size++;
261    return node;
262  }
263
264  /**
265   * Removes the specified node from the linked list. This method is only
266   * intended to be used from the {@code Iterator} classes. See also {@link
267   * LinkedListMultimap#removeAllNodes(Object)}.
268   */
269  private void removeNode(Node<K, V> node) {
270    if (node.previous != null) {
271      node.previous.next = node.next;
272    } else { // node was head
273      head = node.next;
274    }
275    if (node.next != null) {
276      node.next.previous = node.previous;
277    } else { // node was tail
278      tail = node.previous;
279    }
280    if (node.previousSibling == null && node.nextSibling == null) {
281      KeyList<K, V> keyList = keyToKeyList.remove(node.key);
282      keyList.count = 0;
283      modCount++;
284    } else {
285      KeyList<K, V> keyList = keyToKeyList.get(node.key);
286      keyList.count--;
287
288      if (node.previousSibling == null) {
289        keyList.head = node.nextSibling;
290      } else {
291        node.previousSibling.nextSibling = node.nextSibling;
292      }
293
294      if (node.nextSibling == null) {
295        keyList.tail = node.previousSibling;
296      } else {
297        node.nextSibling.previousSibling = node.previousSibling;
298      }
299    }
300    size--;
301  }
302
303  /** Removes all nodes for the specified key. */
304  private void removeAllNodes(@Nullable Object key) {
305    Iterators.clear(new ValueForKeyIterator(key));
306  }
307
308  /** Helper method for verifying that an iterator element is present. */
309  private static void checkElement(@Nullable Object node) {
310    if (node == null) {
311      throw new NoSuchElementException();
312    }
313  }
314
315  /** An {@code Iterator} over all nodes. */
316  private class NodeIterator implements ListIterator<Entry<K, V>> {
317    int nextIndex;
318    Node<K, V> next;
319    Node<K, V> current;
320    Node<K, V> previous;
321    int expectedModCount = modCount;
322
323    NodeIterator(int index) {
324      int size = size();
325      checkPositionIndex(index, size);
326      if (index >= (size / 2)) {
327        previous = tail;
328        nextIndex = size;
329        while (index++ < size) {
330          previous();
331        }
332      } else {
333        next = head;
334        while (index-- > 0) {
335          next();
336        }
337      }
338      current = null;
339    }
340
341    private void checkForConcurrentModification() {
342      if (modCount != expectedModCount) {
343        throw new ConcurrentModificationException();
344      }
345    }
346
347    @Override
348    public boolean hasNext() {
349      checkForConcurrentModification();
350      return next != null;
351    }
352
353    @Override
354    public Node<K, V> next() {
355      checkForConcurrentModification();
356      checkElement(next);
357      previous = current = next;
358      next = next.next;
359      nextIndex++;
360      return current;
361    }
362
363    @Override
364    public void remove() {
365      checkForConcurrentModification();
366      checkRemove(current != null);
367      if (current != next) { // after call to next()
368        previous = current.previous;
369        nextIndex--;
370      } else { // after call to previous()
371        next = current.next;
372      }
373      removeNode(current);
374      current = null;
375      expectedModCount = modCount;
376    }
377
378    @Override
379    public boolean hasPrevious() {
380      checkForConcurrentModification();
381      return previous != null;
382    }
383
384    @Override
385    public Node<K, V> previous() {
386      checkForConcurrentModification();
387      checkElement(previous);
388      next = current = previous;
389      previous = previous.previous;
390      nextIndex--;
391      return current;
392    }
393
394    @Override
395    public int nextIndex() {
396      return nextIndex;
397    }
398
399    @Override
400    public int previousIndex() {
401      return nextIndex - 1;
402    }
403
404    @Override
405    public void set(Entry<K, V> e) {
406      throw new UnsupportedOperationException();
407    }
408
409    @Override
410    public void add(Entry<K, V> e) {
411      throw new UnsupportedOperationException();
412    }
413
414    void setValue(V value) {
415      checkState(current != null);
416      current.value = value;
417    }
418  }
419
420  /** An {@code Iterator} over distinct keys in key head order. */
421  private class DistinctKeyIterator implements Iterator<K> {
422    final Set<K> seenKeys = Sets.<K>newHashSetWithExpectedSize(keySet().size());
423    Node<K, V> next = head;
424    Node<K, V> current;
425    int expectedModCount = modCount;
426
427    private void checkForConcurrentModification() {
428      if (modCount != expectedModCount) {
429        throw new ConcurrentModificationException();
430      }
431    }
432
433    @Override
434    public boolean hasNext() {
435      checkForConcurrentModification();
436      return next != null;
437    }
438
439    @Override
440    public K next() {
441      checkForConcurrentModification();
442      checkElement(next);
443      current = next;
444      seenKeys.add(current.key);
445      do { // skip ahead to next unseen key
446        next = next.next;
447      } while ((next != null) && !seenKeys.add(next.key));
448      return current.key;
449    }
450
451    @Override
452    public void remove() {
453      checkForConcurrentModification();
454      checkRemove(current != null);
455      removeAllNodes(current.key);
456      current = null;
457      expectedModCount = modCount;
458    }
459  }
460
461  /** A {@code ListIterator} over values for a specified key. */
462  private class ValueForKeyIterator implements ListIterator<V> {
463    final Object key;
464    int nextIndex;
465    Node<K, V> next;
466    Node<K, V> current;
467    Node<K, V> previous;
468
469    /** Constructs a new iterator over all values for the specified key. */
470    ValueForKeyIterator(@Nullable Object key) {
471      this.key = key;
472      KeyList<K, V> keyList = keyToKeyList.get(key);
473      next = (keyList == null) ? null : keyList.head;
474    }
475
476    /**
477     * Constructs a new iterator over all values for the specified key starting
478     * at the specified index. This constructor is optimized so that it starts
479     * at either the head or the tail, depending on which is closer to the
480     * specified index. This allows adds to the tail to be done in constant
481     * time.
482     *
483     * @throws IndexOutOfBoundsException if index is invalid
484     */
485    public ValueForKeyIterator(@Nullable Object key, int index) {
486      KeyList<K, V> keyList = keyToKeyList.get(key);
487      int size = (keyList == null) ? 0 : keyList.count;
488      checkPositionIndex(index, size);
489      if (index >= (size / 2)) {
490        previous = (keyList == null) ? null : keyList.tail;
491        nextIndex = size;
492        while (index++ < size) {
493          previous();
494        }
495      } else {
496        next = (keyList == null) ? null : keyList.head;
497        while (index-- > 0) {
498          next();
499        }
500      }
501      this.key = key;
502      current = null;
503    }
504
505    @Override
506    public boolean hasNext() {
507      return next != null;
508    }
509
510    @Override
511    public V next() {
512      checkElement(next);
513      previous = current = next;
514      next = next.nextSibling;
515      nextIndex++;
516      return current.value;
517    }
518
519    @Override
520    public boolean hasPrevious() {
521      return previous != null;
522    }
523
524    @Override
525    public V previous() {
526      checkElement(previous);
527      next = current = previous;
528      previous = previous.previousSibling;
529      nextIndex--;
530      return current.value;
531    }
532
533    @Override
534    public int nextIndex() {
535      return nextIndex;
536    }
537
538    @Override
539    public int previousIndex() {
540      return nextIndex - 1;
541    }
542
543    @Override
544    public void remove() {
545      checkRemove(current != null);
546      if (current != next) { // after call to next()
547        previous = current.previousSibling;
548        nextIndex--;
549      } else { // after call to previous()
550        next = current.nextSibling;
551      }
552      removeNode(current);
553      current = null;
554    }
555
556    @Override
557    public void set(V value) {
558      checkState(current != null);
559      current.value = value;
560    }
561
562    @Override
563    @SuppressWarnings("unchecked")
564    public void add(V value) {
565      previous = addNode((K) key, value, next);
566      nextIndex++;
567      current = null;
568    }
569  }
570
571  // Query Operations
572
573  @Override
574  public int size() {
575    return size;
576  }
577
578  @Override
579  public boolean isEmpty() {
580    return head == null;
581  }
582
583  @Override
584  public boolean containsKey(@Nullable Object key) {
585    return keyToKeyList.containsKey(key);
586  }
587
588  @Override
589  public boolean containsValue(@Nullable Object value) {
590    return values().contains(value);
591  }
592
593  // Modification Operations
594
595  /**
596   * Stores a key-value pair in the multimap.
597   *
598   * @param key key to store in the multimap
599   * @param value value to store in the multimap
600   * @return {@code true} always
601   */
602  @Override
603  public boolean put(@Nullable K key, @Nullable V value) {
604    addNode(key, value, null);
605    return true;
606  }
607
608  // Bulk Operations
609
610  /**
611   * {@inheritDoc}
612   *
613   * <p>If any entries for the specified {@code key} already exist in the
614   * multimap, their values are changed in-place without affecting the iteration
615   * order.
616   *
617   * <p>The returned list is immutable and implements
618   * {@link java.util.RandomAccess}.
619   */
620  @Override
621  public List<V> replaceValues(@Nullable K key, Iterable<? extends V> values) {
622    List<V> oldValues = getCopy(key);
623    ListIterator<V> keyValues = new ValueForKeyIterator(key);
624    Iterator<? extends V> newValues = values.iterator();
625
626    // Replace existing values, if any.
627    while (keyValues.hasNext() && newValues.hasNext()) {
628      keyValues.next();
629      keyValues.set(newValues.next());
630    }
631
632    // Remove remaining old values, if any.
633    while (keyValues.hasNext()) {
634      keyValues.next();
635      keyValues.remove();
636    }
637
638    // Add remaining new values, if any.
639    while (newValues.hasNext()) {
640      keyValues.add(newValues.next());
641    }
642
643    return oldValues;
644  }
645
646  private List<V> getCopy(@Nullable Object key) {
647    return unmodifiableList(Lists.newArrayList(new ValueForKeyIterator(key)));
648  }
649
650  /**
651   * {@inheritDoc}
652   *
653   * <p>The returned list is immutable and implements
654   * {@link java.util.RandomAccess}.
655   */
656  @Override
657  public List<V> removeAll(@Nullable Object key) {
658    List<V> oldValues = getCopy(key);
659    removeAllNodes(key);
660    return oldValues;
661  }
662
663  @Override
664  public void clear() {
665    head = null;
666    tail = null;
667    keyToKeyList.clear();
668    size = 0;
669    modCount++;
670  }
671
672  // Views
673
674  /**
675   * {@inheritDoc}
676   *
677   * <p>If the multimap is modified while an iteration over the list is in
678   * progress (except through the iterator's own {@code add}, {@code set} or
679   * {@code remove} operations) the results of the iteration are undefined.
680   *
681   * <p>The returned list is not serializable and does not have random access.
682   */
683  @Override
684  public List<V> get(final @Nullable K key) {
685    return new AbstractSequentialList<V>() {
686      @Override
687      public int size() {
688        KeyList<K, V> keyList = keyToKeyList.get(key);
689        return (keyList == null) ? 0 : keyList.count;
690      }
691
692      @Override
693      public ListIterator<V> listIterator(int index) {
694        return new ValueForKeyIterator(key, index);
695      }
696    };
697  }
698
699  @Override
700  Set<K> createKeySet() {
701    @WeakOuter
702    class KeySetImpl extends Sets.ImprovedAbstractSet<K> {
703      @Override
704      public int size() {
705        return keyToKeyList.size();
706      }
707
708      @Override
709      public Iterator<K> iterator() {
710        return new DistinctKeyIterator();
711      }
712
713      @Override
714      public boolean contains(Object key) { // for performance
715        return containsKey(key);
716      }
717
718      @Override
719      public boolean remove(Object o) { // for performance
720        return !LinkedListMultimap.this.removeAll(o).isEmpty();
721      }
722    }
723    return new KeySetImpl();
724  }
725
726  /**
727   * {@inheritDoc}
728   *
729   * <p>The iterator generated by the returned collection traverses the values
730   * in the order they were added to the multimap. Because the values may have
731   * duplicates and follow the insertion ordering, this method returns a {@link
732   * List}, instead of the {@link Collection} specified in the {@link
733   * ListMultimap} interface.
734   */
735  @Override
736  public List<V> values() {
737    return (List<V>) super.values();
738  }
739
740  @Override
741  List<V> createValues() {
742    @WeakOuter
743    class ValuesImpl extends AbstractSequentialList<V> {
744      @Override
745      public int size() {
746        return size;
747      }
748
749      @Override
750      public ListIterator<V> listIterator(int index) {
751        final NodeIterator nodeItr = new NodeIterator(index);
752        return new TransformedListIterator<Entry<K, V>, V>(nodeItr) {
753          @Override
754          V transform(Entry<K, V> entry) {
755            return entry.getValue();
756          }
757
758          @Override
759          public void set(V value) {
760            nodeItr.setValue(value);
761          }
762        };
763      }
764    }
765    return new ValuesImpl();
766  }
767
768  /**
769   * {@inheritDoc}
770   *
771   * <p>The iterator generated by the returned collection traverses the entries
772   * in the order they were added to the multimap. Because the entries may have
773   * duplicates and follow the insertion ordering, this method returns a {@link
774   * List}, instead of the {@link Collection} specified in the {@link
775   * ListMultimap} interface.
776   *
777   * <p>An entry's {@link Entry#getKey} method always returns the same key,
778   * regardless of what happens subsequently. As long as the corresponding
779   * key-value mapping is not removed from the multimap, {@link Entry#getValue}
780   * returns the value from the multimap, which may change over time, and {@link
781   * Entry#setValue} modifies that value. Removing the mapping from the
782   * multimap does not alter the value returned by {@code getValue()}, though a
783   * subsequent {@code setValue()} call won't update the multimap but will lead
784   * to a revised value being returned by {@code getValue()}.
785   */
786  @Override
787  public List<Entry<K, V>> entries() {
788    return (List<Entry<K, V>>) super.entries();
789  }
790
791  @Override
792  List<Entry<K, V>> createEntries() {
793    @WeakOuter
794    class EntriesImpl extends AbstractSequentialList<Entry<K, V>> {
795      @Override
796      public int size() {
797        return size;
798      }
799
800      @Override
801      public ListIterator<Entry<K, V>> listIterator(int index) {
802        return new NodeIterator(index);
803      }
804    }
805    return new EntriesImpl();
806  }
807
808  @Override
809  Iterator<Entry<K, V>> entryIterator() {
810    throw new AssertionError("should never be called");
811  }
812
813  @Override
814  Map<K, Collection<V>> createAsMap() {
815    return new Multimaps.AsMap<K, V>(this);
816  }
817
818  /**
819   * @serialData the number of distinct keys, and then for each distinct key:
820   *     the first key, the number of values for that key, and the key's values,
821   *     followed by successive keys and values from the entries() ordering
822   */
823  @GwtIncompatible("java.io.ObjectOutputStream")
824  private void writeObject(ObjectOutputStream stream) throws IOException {
825    stream.defaultWriteObject();
826    stream.writeInt(size());
827    for (Entry<K, V> entry : entries()) {
828      stream.writeObject(entry.getKey());
829      stream.writeObject(entry.getValue());
830    }
831  }
832
833  @GwtIncompatible("java.io.ObjectInputStream")
834  private void readObject(ObjectInputStream stream) throws IOException, ClassNotFoundException {
835    stream.defaultReadObject();
836    keyToKeyList = Maps.newLinkedHashMap();
837    int size = stream.readInt();
838    for (int i = 0; i < size; i++) {
839      @SuppressWarnings("unchecked") // reading data stored by writeObject
840      K key = (K) stream.readObject();
841      @SuppressWarnings("unchecked") // reading data stored by writeObject
842      V value = (V) stream.readObject();
843      put(key, value);
844    }
845  }
846
847  @GwtIncompatible("java serialization not supported")
848  private static final long serialVersionUID = 0;
849}