I have a gigantic data set which I’ve to store into a collection and need to find whethere any duplicates in there or not.
The data size could be more than 1 million. I know I can store more element in ArrayList comapre to a Map.
My questions are:
- is searching key in a
Mapfaster than searching in sortedArrayList? - is searching Key in
HashMapis faster thanTreeMap? - Only in terms of space required to store
nelements, which would be more efficient between aTreeMapand aHashMapimplementation?
1) Yes. Searching an
ArrayListis O(n) on average. The performance of key lookups in a Map depends on the specific implementation. You could write an implementation ofMapthat is O(n) or worse if you really wanted to, but all the implementations in the standard library are faster than O(n).2) Yes.
HashMapis O(1) on average for simple key lookups.TreeMapis O(log(n)).Class HashMap<K,V>Class TreeMap<K,V>3) The space requirements will be O(n) in both cases. I’d guess the
TreeMaprequires slightly more space, but only by a constant factor.