1

What is the time complexity of put() and get() in Guava ListMultimap or Guava ArrayListMultimap ?

I have read the below Guava Documentation, However, the time complexity of these operations is not mentioned.

ListMultimap

ArrayListMultimap

Is the time complexity same as that of Hashmap (O(1) for both put() and get())?

Mehraj Malik
  • 11,335
  • 12
  • 46
  • 74
Divyansh
  • 23
  • 2
  • 1
    Did you examine the source for those classes to see what they do? – Jim Garrison Feb 27 '18 at 06:35
  • The `ArrayListMultimap` just uses an `ArrayList` internally. – Ben Feb 27 '18 at 06:41
  • @JimGarrison Yes, I read about it's super interface Multimap [link](https://google.github.io/guava/releases/snapshot/api/docs/com/google/common/collect/Multimap.html). But nothing has been mentioned about time complexity of methods in the super interface. – Divyansh Feb 27 '18 at 06:43
  • @Ben Does that mean it's an O(n) operation if it is using ArrayList? So, If I want O(1) time complexity in put() and get() operation, should I use HashMultimap [link](https://google.github.io/guava/releases/snapshot/api/docs/com/google/common/collect/HashMultimap.html) – Divyansh Feb 27 '18 at 06:46

1 Answers1

3

This is described, though a bit indirectly, on guava's general documentation for multimap. Specifically, in the "Implementations" section, it says that for ArrayListMultimap, the keys behave as HashMap, and the values behave as ArrayList.

Thus, the get and put are both O(1) (with the usual caveats about that claim that accompany HashMap). For get, it's just an O(1) operation that gets the ArrayList; for put, it's that same O(1) get, and then another O(1) put (amortized, as ArrayList additions always are).

yshavit
  • 39,951
  • 7
  • 75
  • 114