Prompt the best collection in terms of performance in a situation where you need to create and replenish it very often. In this case, the order of adding elements should be preserved, and the elements in the collection are few, maximum 30 pieces.

  • I think for your purposes, ConcurrentHashMap will do. - And
  • one
    @And does not guarantee the preservation of order, and the author did not write about multithreading. - Sergey Gornostaev
  • What does it mean to create often? You cannot often create one collection. ¯_ (ツ) _ / ¯ - Anton Sorokin
  • Choose any. For example LinkedList. With 30 elements there is absolutely no difference in terms of performance - Roman Danilov
  • Often create in the sense of an object will create a new collection of this very often. Separate elements will be added to the collection, if more precisely, individual lines. - ProstoCoder

4 answers 4

Choose: How to choose a collection?

Only Vector and Stack are no longer used, instead of them (to replace a collection with synchronization) - CopyOnWriteArrayList .

PS Looks like you need LinkedList .

You need a list. At 30 elements take ArrayList. Even when you insert a shift into the beginning, 29 elements are quite comparable in speed (although, maybe slower, did not check) with memory allocation and permutation of 6 links, besides, you will receive data in the processor cache, which will ensure quick access to them later.

  • Plus The maximum use of cache is so apparently driving. - Alexander Petrov
  • I previously found a recommendation to use ArrayDeque, and what do you think about this? - ProstoCoder

If the main operation is an addition, then use the list because insertion of the item in O (1) occurs.

  • 2
    Which list exactly? After all, they are different in speed, in order to preserve the order of addition, etc., they have many differences. - Anton Sorokin

ArrayList if inserting elements in the beginning, LinkedList if in the middle. Although, for 30 elements, as stated above, you can use any of these options

  • With a fright Linked to this question? - Qwertiy
  • Already fixed, on the contrary :) - Nikita
  • Yes, the opposite is better :) - Qwertiy