简体   繁体   中英

ceiling() with TreeMultimap

  • Is it possible to extend TreeMultimap from Google's guava to get some king odd ceiling function? ceiling(key) would return the smallest key greater than the given one. (I know I could get an ordered set view and just look, but I would prefer something with beter time complexity, like a balanced binary search tree offers)
  • Is there any other library that would implement a balanced binary search tree and allow that?
  • What is the complexity of common operations of TreeMultimap?
multimap.keySet().ceiling(key)

does it pretty directly, but you need Java 6 and the most recent Guava release, 14.0, which is when TreeMultimap.keySet() started returning NavigableSet . The complexity is O(log #keys), exactly as you would expect.

K ceiling(K key, TreeMultimap<K,V> map) {
  SortedSet<K> keyset = map.keySet();
  SortedSet<K> head = keyset.headSet(key);
  return headSet.isEmpty() ? null : head.last();    
}

The documentation doesn't mention any time guarantees for the operations, but I would expect it to run in logarithmic time since both keySet and headSet appear to return views of the underlying data rather than building new collections themselves.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM