ArrayList vs HashMap - a lot of iteration and manipulation of objects

Basically, I have some data structure from a ton of objects, and there will be multiple streams available to this structure and will need to take that into account.

A lot of iteration and manipulation of objects will need to be done all the time (each iteration of the main loop may result in every object in the data structure being modified in the worst case, nothing will change in the best / normal case).

I am currently using it CopyOnWriteArrayList

as my framework. Also, on every iteration, I try to avoid adding duplicates to keep the list size down.

Using locks / synchronization is not ideal as I want to avoid threading support for these operations.

As far as I can tell, my options for doing this are as follows:

  • Run a check contains()

    for every item you add
  • Build HashSet

    from a list and convert it (essentially removing all duplicates)
  • Use ConcurrentHashMap

    instead of list for data structure
  • Something else?

I know it's ArrayLists

much better with iteration, while object manipulation and duplicate checking are better handled strictly with HashMap

. Since I will need about me, I am wondering what is the best solution here.

I should also point out that the ordering of the elements is not a problem.

Edit: To clarify this, the collection will contain items that will continually be added, removed and modified. To what extent this depends on each particular runtime (based on usually random events), so I am careful about making assumptions about how often this will happen. The only thing that is guaranteed is that the collection will run completely each time, doing multiple checks for each item.

+3


source to share


1 answer


This answer addresses your concurrency issues:

A lot of iteration and manipulation of objects will need to be done all the time (each iteration of the main loop can result in every object in the data structure being modified in the worst case, nothing in the best / normal case).

Will the collection be changed? Unless you are just choosing which collection ever makes the most sense and syncs with objects. When they are inside a collection, you do not benefit from synchronization from CopyOnWriteArraylist

or ConcurrentHashMap

.



If the collection is changed, the subsequent execution, how often?

If not much use CopyOnWriteArrayList. If a little then choose based on the highest search performance.

+1


source







All Articles