EvictingQueue 是什么?
官方说明:
A non-blocking queue which automatically evicts elements from the head of the queue when attempting to add new elements onto the queue and it is full. This queue orders elements FIFO (first-in-first-out). This data structure is logically equivalent to a circular buffer (i.e., cyclic buffer or ring buffer).
以下上测试代码:
@Test
public void testGuava001() {
EvictingQueue<Integer> q = EvictingQueue.create(4);
q.add(1);
q.add(2);
q.add(3);
q.add(4);
q.add(5);
int first = q.peek();
Assert.assertEquals(first, 2);
Assert.assertEquals(q.size(), 4);
}
说明: q的容量只有4个,当继续往里面放5这个元素的时候,里面的最早的那个元素会被去除,所以最上面的元素是2,size仍然是4.