TreeMultimap操作的时间复杂度

时间:2013-12-24 21:49:42

标签: java time-complexity

TreeMultimap中put(key, value)get(key)的时间复杂度是多少?

文档中未提及: http://google-collections.googlecode.com/svn/trunk/javadoc/com/google/common/collect/TreeMultimap.html

1 个答案:

答案 0 :(得分:4)

检查grepcode

@Override public boolean put(K key, V value) {
    return super.put(key, value);
  }

supercom.google.common.collect.AbstractMultimap

  public boolean put(@Nullable K key, @Nullable V value) {
    Collection<V> collection = getOrCreateCollection(key);

    if (collection.add(value)) {
      totalSize++;
      return true;
    } else {
      return false;
    }
  }

The data structure that drives this is

private transient Map<K, Collection<V>> map;

外部地图是TreeMap,您可以从跟踪构造函数验证。

createCollection是抽象的:

 protected abstract Collection<V> createCollection();

实施使用TreeSet

@Override SortedSet<V> createCollection() {
return (valueComparator == null)
    ? new TreeSet<V>() : new TreeSet<V>(valueComparator);

}

因此put

  1. getTreeMap
  2. put TreeMapTreeSet
  3. 这些是log(n),因此TreeMultimap也是log(n)