I have implemented a method which simply loops around a set of CSV files that contain data on a number of different module. This then adds the \'moduleName\' into a hashSet.
It depends upon the usage of the data structure.
You are storing the data in HashSet
, and for your case for storage HashSet
is better than ArrayList
(as you do not want duplicate entries). But just storing is not the usual intent.
It depends as how you wish to read and process the stored data. If you want sequential access or random index based access then ArrayList
is better or if ordering does not matter then HashSet
is better.
If ordering matters but you want to do lot of modifications (additions and deletions) the LinkedList is better.
For accessing a particular element HashSet
will have time complexity as O (1) and if you would have used ArrayList
it would have been O (N) as you yourself have pointed out you would have to iterate
through the list and see if the element is not present.