i am trying to study the difference between sets , arrays and linked lists in Java in terms of performace so the example i am working on is
if we need to see the common objects between two arrays , two set and two linked list ,
in the arrays we apply the for loop and compare , in set we use interscet or union?
but there is a huge difference in timing, any ideas?
i am trying to study the difference between sets , arrays and linked lists
Share
There is a difference between intersecting sets and intersecting lists or arrays.
When you intersect two sets, you need to iterate over one of them, and see if there is a corresponding member in the other set. Retrieval time for set is O(1), so the intersection is O(n).
When intersecting a list or an array, the intersection is O(n^2), since for each member in one list/array, you need to iterate over the whole other list/array