When its comes to the behavior of ArrayList
and HashSet
they are completely different classes.
ArrayList
Does not validate duplicates.get()
isO(1)
contains()
isO(n)
but you have fully control over the order of the entries.get add contains next remove(0) iterator.remove ArrayList O(1) O(1) O(n) O(1) O(1) O(1)
- Not thread safe and to make it thread safe you have to use
Collections.synchronizedList(...)
HashSet
ensures there are no duplicates.- Gives you an
O(1)
contains()
method but doesn’t preserve order.add contains next notes HashSet O(1) O(1) O(h/n) h is the table
- Not thread safe and to make it thread safe you have to use
Collections.synchronizedSet(...)