Basically, I have some data structure of a ton of objects, and this structure will be accessed by multiple threads and will need to account for that.
A lot of iteration and object manipulation will need to be performed constantly (each main loop iteration can result in every single object in the data structure being modified in a worst case, nothing modified in best/normal case).
Currently, I am using a CopyOnWriteArrayList
as my structure. Additionally, on each iteration, I make sure not to add duplicates, in an attempt to keep the size of the list down.
Using locks/synchronized is not ideal as I want to avoid holding up the threads for these operations.
As far as I can tell, my options for this are as follows:
contains()
check for each element to be addedHashSet
from the list and convert it back (essentially removing all duplicates)ConcurrentHashMap
instead of a list for the data structureI am aware that ArrayLists
are much better with iteration while object manipulation and duplicate checking are better handled by strictly using a HashMap
. Since my case will need both, I'm wondering what the best solution is here.
I should also mention that the ordering of the elements is a non-issue.
Edit: To clarify this further, the collection will be having elements constantly added, removed, and modified. To which degree depends on each specific run time (based off of generally random events), so I'm cautious about making any assumptions about how often it will occur. The only thing that is guaranteed to happen is that the collection will be iterated through completely each time, performing multiple checks on each element.
This answer addresses your concurrency concerns:
A lot of iteration and object manipulation will need to be performed constantly (each main loop iteration can result in every single object in the data structure being modified in worst case, nothing modified in best/normal case).
Will the collection be modified? If not just choose which ever collection makes most sense and synchronize on the objects. Once they are inside the collection you get no synchronization benefits from the CopyOnWriteArraylist
or ConcurrentHashMap
.
If the collection will be modified the follow up is, how often?
If a lot do not use a CopyOnWriteArrayList. If a little then choose based on highest search performance.