Size-limited queue that holds last N elements in Java

后端 未结 8 1569
灰色年华
灰色年华 2020-11-22 13:13

A very simple & quick question on Java libraries: is there a ready-made class that implements a Queue with a fixed maximum size - i.e. it always allows addi

8条回答
  •  被撕碎了的回忆
    2020-11-22 13:28

    You can use a MinMaxPriorityQueue from Google Guava, from the javadoc:

    A min-max priority queue can be configured with a maximum size. If so, each time the size of the queue exceeds that value, the queue automatically removes its greatest element according to its comparator (which might be the element that was just added). This is different from conventional bounded queues, which either block or reject new elements when full.

提交回复
热议问题