java - Arraylist vs Array for finding element? -
which efficient way of finding element in terms of performance. have 100's of strings. need find whether specified string available in bulk strings. have contains() method in arraylist, need iterate through array same purpose. explain, best way of doing in terms of performance.
say have 100's of strings. need find whether specified string available in bulk strings.
that sounds want hashset<string>
- not list or array. @ least, that's case if hundreds of strings same every time want search. if you're searching within different set of strings every time, you're not going better o(n) if receive set in arbitrary order.
in general, checking containment in list/array o(n) operation, whereas in hash-based data structure it's o(1). of course there's cost of performing hashing , equality checking, that's different matter.
another option sorted list, o(log n).
if care ordering, might want consider linkedhashset<string>
, maintains insertion order still has o(1) access. (it's linked list combined hash set.)
Comments
Post a Comment