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?
Mapisn’t just a list of key-value pairs, it is a unique mapping from keys to values. So when you change fromMaptoList, you are allowing duplicates where you previously didn’t. On the other hand, aSetis exactly aMapwithout the values. So consider using aHashSet.As for the search complexities:
list.containsis O(n),hashSet.containsis O(1), andtreeSet.containsis O(log n).For general information on now
HashMapworks, google for "hashtable". ForTreeMap, 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 caseHashSetis probably the best choice.