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.checkNotNull;
 
 
Implementation of Multimap whose keys and values are ordered by their natural ordering or by supplied comparators. In all cases, this implementation uses java.lang.Comparable.compareTo(java.lang.Object) or java.util.Comparator.compare(java.lang.Object,java.lang.Object) instead of java.lang.Object.equals(java.lang.Object) to determine equivalence of instances.

Warning: The comparators or comparables used must be consistent with equals as explained by the java.lang.Comparable class specification. Otherwise, the resulting multiset will violate the general contract of SetMultimap, which it is specified in terms of java.lang.Object.equals(java.lang.Object).

The collections returned by keySet and asMap iterate through the keys according to the key comparator ordering or the natural ordering of the keys. Similarly, get, removeAll, and replaceValues return collections that iterate through the values according to the value comparator ordering or the natural ordering of the values. The collections generated by entries, keys, and values iterate across the keys according to the above key ordering, and for each key they iterate across the values according to the value ordering.

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.

Null keys and values are permitted (provided, of course, that the respective comparators support them). 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 com.google.common.collect.Multimaps.synchronizedSortedSetMultimap(com.google.common.collect.SortedSetMultimap).

See the Guava User Guide article on Multimap.

Author(s):
Jared Levy
Since:
2.0 (imported from Google Collections Library)
 
 @GwtCompatible(serializable = true, emulated = true)
 public class TreeMultimap<K, V> extends AbstractSortedSetMultimap<K, V> {
   private transient Comparator<? super K> keyComparator;
   private transient Comparator<? super V> valueComparator;

  
Creates an empty TreeMultimap ordered by the natural ordering of its keys and values.
 
   public static <K extends Comparable, V extends Comparable>
       TreeMultimap<K, V> create() {
     return new TreeMultimap<K, V>(Ordering.natural(), Ordering.natural());
   }

  
Creates an empty TreeMultimap instance using explicit comparators. Neither comparator may be null; use com.google.common.collect.Ordering.natural() to specify natural order.

Parameters:
keyComparator the comparator that determines the key ordering
valueComparator the comparator that determines the value ordering
 
   public static <K, V> TreeMultimap<K, V> create(
       Comparator<? super K> keyComparator,
       Comparator<? super V> valueComparator) {
     return new TreeMultimap<K, V>(checkNotNull(keyComparator),
        checkNotNull(valueComparator));
  }

  
Constructs a TreeMultimap, ordered by the natural ordering of its keys and values, with the same mappings as the specified multimap.

Parameters:
multimap the multimap whose contents are copied to this multimap
  public static <K extends Comparable, V extends Comparable>
      TreeMultimap<K, V> create(Multimap<? extends K, ? extends V> multimap) {
    return new TreeMultimap<K, V>(Ordering.natural(), Ordering.natural(),
        multimap);
  }
  TreeMultimap(Comparator<? super K> keyComparator,
      Comparator<? super V> valueComparator) {
    super(new TreeMap<K, Collection<V>>(keyComparator));
    this. = keyComparator;
    this. = valueComparator;
  }
  private TreeMultimap(Comparator<? super K> keyComparator,
      Comparator<? super V> valueComparator,
      Multimap<? extends K, ? extends V> multimap) {
    this(keyComparatorvalueComparator);
    putAll(multimap);
  }

  

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

Returns:
a new TreeSet containing a collection of values for one key
    return new TreeSet<V>();
  }

  
Returns the comparator that orders the multimap keys.
  public Comparator<? super K> keyComparator() {
    return ;
  }
  public Comparator<? super V> valueComparator() {
    return ;
  }

  

Because a TreeMultimap has unique sorted keys, this method returns a java.util.SortedSet, instead of the java.util.Set specified in the com.google.common.collect.Multimap interface.

  @Override public SortedSet<K> keySet() {
    return (SortedSet<K>) super.keySet();
  }

  

Because a TreeMultimap has unique sorted keys, this method returns a java.util.SortedMap, instead of the java.util.Map specified in the com.google.common.collect.Multimap interface.

  @Override public SortedMap<K, Collection<V>> asMap() {
    return (SortedMap<K, Collection<V>>) super.asMap();
  }

  

SerialData:
key comparator, value comparator, number of distinct keys, and then for each distinct key: the key, number of values for that key, and key values
  @GwtIncompatible("java.io.ObjectOutputStream")
  private void writeObject(ObjectOutputStream streamthrows IOException {
    stream.defaultWriteObject();
    stream.writeObject(keyComparator());
    stream.writeObject(valueComparator());
    Serialization.writeMultimap(thisstream);
  }
  @GwtIncompatible("java.io.ObjectInputStream")
  @SuppressWarnings("unchecked"// reading data stored by writeObject
  private void readObject(ObjectInputStream stream)
      throws IOExceptionClassNotFoundException {
    stream.defaultReadObject();
     = checkNotNull((Comparator<? super K>) stream.readObject());
     = checkNotNull((Comparator<? super V>) stream.readObject());
    setMap(new TreeMap<K, Collection<V>>());
    Serialization.populateMultimap(thisstream);
  }
  @GwtIncompatible("not needed in emulated source")
  private static final long serialVersionUID = 0;
New to GrepCode? Check out our FAQ X