I'm writing an algorithm where I look for pairs of values which when added together results in another value I'm looking for.
I figured out that using a Map
will speed up my algorithm from O(n²). I later realized that I don't really use the values contained in my Map
so a List
will suffice.
I did a power search on Google but I did not find any information on the asymptotic running time of those methods in the title of my question.
Can you point out where should I look for such information?
So a map is really faster if you need to check the key appearance in a collection, and do not need to keep the order (there is a SortedHashMap for that, but I don't know it's performance), but it will take more memory.
The ArrayList always gives O(1) performance in best case or worst-case time complexity. The HashMap get() method has O(1) time complexity in the best case and O(n) time complexity in worst case. ArrayList has any number of null elements. HashMap allows only one null Key and lots of null values.
It takes approximately the same amount of time to call get and containsKey, and it's virtually guaranteed that after you call containsKey, you're going to call get anyways, so you may as well cut out the middle man.
Use a map when you want your data structure to represent a mapping for keys to values. Use a list when you want your data to be stored in an arbitrary, ordered format.
I later realized that I don't really use the values contained in my
Map
so aList
will suffice.
Map
isn't just a list of key-value pairs, it is a unique mapping from keys to values. So when you change from Map
to List
, you are allowing duplicates where you previously didn't. On the other hand, a Set
is exactly a Map
without the values. So consider using a HashSet
.
As for the search complexities:
list.contains
is O(n), hashSet.contains
is O(1), and treeSet.contains
is O(log n).
For general information on now HashMap
works, google for "hashtable". For TreeMap
, google for "binary tree" or similar. Wikipedia has good entries on these subjects.
Be careful, however, to avoid the class Hashtable
. It's an archaeological artefact in the modern library. For your case HashSet
is probably the best choice.
Map
and List
are interfaces, so there is no information on their implementation nor their performance. But if you use the most current implementations (LinkedList
or ArrayList
for List
, and HashMap
for Map
), the contains()
method must, in the worst case, go through the entire list, and compare your element with each entry. It is an O(n) operation.
If you use an HashMap
, the implementation is radically different : the HashMap
contains an array with more entries than elements in it (in practice, you have an array size of between 4n/3 an 3n/2 for n elements in the map). It computes the hash of the key, which is an int, and wrap it between 0 and your array size (let's say this number is i
). Then it will put the element at the index i
of the array (or i+1
, i+2
… if previous indexes are already taken). So, when you check for the key presence with containsKey
, it will re-compute the hash and the i
value, and check the i
, i+1
… indexes until it finds an empty array cell. Theorically, you can have an O(n) worst-case, if the array is almost full, are all the keys have almost identicals i
values, but with a good hash function, you have constant-time contains
and get
functions. (However, adding elements is fast if you don't need to resize the array, which is REALLY slow - I think you need to recompute the indexes of each key).
So a map is really faster if you need to check the key appearance in a collection, and do not need to keep the order (there is a SortedHashMap
for that, but I don't know it's performance), but it will take more memory.
Also, if you don't need the key-value thing, you can use a HashSet
(which is internally the same as an HashMap
).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With