Best collection to use?

I read the log files, but not all lines want to be processed immediately. I use a queue / buffer to store strings while they are waiting for processing.

This queue is regularly scanned for certain lines - when they are found, they are removed from the queue (they can be anywhere in it). When there is no specific line to be found, the lines are taken out of the queue one by one for processing.

Therefore, the queue requires the following:

  • The ability to resize (or create such an impression)
  • Items removed from anywhere
  • Elements added (will always be at the end of the queue)
  • Scan fast
  • Depending on performance, a pointer to where it was on the last scan.

I originally wrote the code when I had little Java or API experience, and just used ArrayList because I knew that it would work (not necessarily because this is the best option).

Currently, its performance is getting worse and more and more magazines need to be processed - so which collection would you recommend to use in this situation? There is always the opportunity to write too.

thank

+3
source share
8 answers

LinkedHashSet may be of interest. This is a HashSet efficient, but it also supports LinkedList to allow a predictable iteration order, and therefore can also be used as a FIFO queue, with the good added advantage that it cannot contain duplicate entries.

HashSet, ( ) O (1), equals()

+6

, LinkedList . , , ArrayList.

- , PriorityQueue .

+4

, , - , . Log (n) holdskey, , - , .

+2

, ( ). , , ( ).

, :

HashMap<String,LinkedList<String>>

LinkedList , .

, X, / ( , , x, , x, - /).

, , , ? , Queue ?

+1

, , , SortedSet, TreeSet. log (n) , .

0

, , .

java.lang.concurrent.

PriorityBlockingQueue, LinkedBlockingQueue, .

0

AVI, . , , . , .

0

Guava .

Guava Google, Java: , , , concurrency , , , - ..

0

Source: https://habr.com/ru/post/1699503/


All Articles