Guide to Apache Commons CircularFifoQueue – Apache Commons CircularFifoQueue指南

最后修改: 2017年 7月 30日

中文/混合/英文(键盘快捷键:t)

1. Overview

1.概述

In this quick tutorial, we’ll have a look at the CircularFifoQueue data structure provided in the collections4.queue package of the Apache Commons Collections library.

在这个快速教程中,我们将看看Apache Commons Collections库的collections4.queue包中提供的CircularFifoQueue数据结构。

CircularFifoQueue<E> implements the Queue<E> interface and is a fixed-size, non-blocking queuewhen you add an element to a queue that is full, the oldest element is removed to make room for the new element.

CircularFifoQueue<E>实现了Queue<E>接口,是一个固定大小的非阻塞的队列当你向已满的队列添加一个元素时,最旧的元素被移除以腾出空间给新元素

2. Maven Dependency

2.Maven的依赖性

For Maven projects, we need to add the required dependency:

对于Maven项目,我们需要添加所需的依赖性。

<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-collections4</artifactId>
    <version>4.1</version>
</dependency>

You can find the latest version of this library on Maven Central.

您可以在Maven Central上找到该库的最新版本。

3. Constructors

3.构造器

To create a CircularFifoQueue object, we can use the default constructor, which creates a queue with the default size of 32:

要创建一个CircularFifoQueue对象,我们可以使用默认构造函数,它可以创建一个默认大小为32的队列。

CircularFifoQueue<String> bits = new CircularFifoQueue();

If we know the desired maximum size of our queue, we can use the constructor that takes an int as an argument to specify the size:

如果我们知道我们的队列所需的最大尺寸,我们可以使用接受一个int作为参数的构造函数来指定尺寸。

CircularFifoQueue<String> colors = new CircularFifoQueue<>(5);

There is also an option to create a CircularFifoQueue object by giving the constructor a collection as an argument.

还有一个选项是通过给构造函数一个集合作为参数来创建一个CircularFifoQueue对象。

In this case, the queue will be filled with the elements of the collection, and its size will be the same as the size of the collection:

在这种情况下,队列将被集合中的元素填满,其大小将与集合的大小相同。

CircularFifoQueue<String> daysOfWeek = new CircularFifoQueue<>(days);

Note: since this queue is already full when constructed, any addition will cause the first created element to be discarded.

注意:由于这个队列在构建时已经满了,任何增加将导致第一个创建的元素被丢弃。

4. Adding Elements

4.添加元素

As with any Queue implementation, we can add elements by using the add and offer methods. The Queue JavaDoc specifies that the offer method is intended to be used when operating with a queue that is capacity-restricted.

与任何 Queue 实现一样,我们可以通过使用 addoffer 方法添加元素。Queue JavaDoc规定,offer方法打算在操作容量受限的队列时使用。

However, since the CircularFifoQueue is nonblocking, inserts cannot fail. As a result, its add and offer methods exhibit the same behavior.

然而,由于CircularFifoQueue是非阻塞的,所以插入不会失败。因此,其addoffer方法表现出相同的行为。

Let’s see how we can add elements to our colors queue using the add method:

让我们看看如何使用add方法向我们的colors队列添加元素。

colors.add("Red");
colors.add("Blue");
colors.add("Green");

And let’s add some elements using the offer method:

让我们使用offer方法添加一些元素。

colors.offer("White");
colors.offer("Black");

5. Removing and Retrieving Elements

5.删除和检索元素

The CircularFifoQueue class offers us a few methods that can be useful when we need to manipulate the queue’s elements. Some methods are used for getting elements from the queue, some for removing elements, and some for doing both operations at the same time.

CircularFifoQueue类为我们提供了一些方法,当我们需要操作队列中的元素时,这些方法会很有用。一些方法用于从队列中获取元素,一些用于移除元素,还有一些用于同时进行两种操作。

5.1. Peek Method

5.1.Peek方法

The peek method is nondestructive and returns the head of the queue.

peek方法是非破坏性的,返回队列的头部

This method will always return the same element as long as there haven’t been any changes to the elements in the queue between calls. If the queue is empty, peek will return null:

只要在两次调用之间队列中的元素没有任何变化,该方法将始终返回相同的元素。如果队列是空的,peek将返回null:

String colorsHead = colors.peek();

5.2. Element Method

5.2.元素方法

The element method is similar to peek — it returns the current head of the queue.

element方法类似于peek–它返回队列的当前头部

However, the element method throws an exception if the queue is empty:

然而,如果队列是空的,element方法会抛出一个异常。

colorsHead = colors.element();

5.3. Get Method

5.3.Get方法

When we need to get a certain element from the queue, we can use the get method. This method takes the index of the desired element as an argument. The index of the queue is zero-based.

当我们需要从队列中获取某个元素时,我们可以使用get方法。这个方法将所需元素的索引作为参数。队列的索引是基于零的。

Let’s get an element from the colors queue that we filled with elements earlier:

让我们从colors队列中获取一个元素,我们之前已经用元素填充了这个队列。

String color = colors.get(1);

This will return “Blue“.

这将返回”蓝色“。

Now let’s add three elements to our queue and check this result again:

现在让我们在队列中添加三个元素,并再次检查这个结果。

colors.add("Orange");
colors.add("Violet");
colors.add("Pink");
		
color = colors.get(1);

This time, the get method returns “Black“. This is because our queue was created with a limited size of five, and the first three elements (“Red“, “Blue“, “Green“) were removed with the addition of the new ones.

这一次,get方法返回”Black“。这是因为我们的队列在创建时有5个大小的限制,前三个元素(”Red“, “Blue“, “Green“)随着新元素的加入而被移除。

5.4. Poll Method

5.4.Poll方法

The poll method removes the head element of the queue and returns that element. If the queue has no elements, the poll method returns null:

poll方法移除队列的头部元素并返回该元素。如果队列中没有元素,poll方法会返回null:

colorsHead = colors.poll();

5.5. Remove Method

5.5.移除方法

The remove method operates much like the poll method — it returns the head of the queue and removes the returned element. However, if the queue is empty, remove will throw an exception:

remove方法 操作与poll方法类似 – 它返回队列的头部,并删除返回的元素。然而,如果队列是空的,remove将抛出一个异常

colorsHead = colors.remove();

5.6. Clear Method

5.6.清除方法

We can use the clear method when we want to empty our queue:

当我们想清空队列时,我们可以使用clear方法。

colors.clear();

6. Checking Methods

6.检查方法

After seeing how we can add, remove and retrieve elements of the queue, let’s see what the class has to offer regarding checking its size and capacity. We will use the queues created in previous sections in our examples.

在看到我们如何添加、删除和检索队列的元素之后,让我们看看这个类在检查其大小和容量方面有什么作用。我们将在我们的例子中使用前几节中创建的队列。

In general, we have at our disposal two methods for checking the size of our queue — one for getting the maximum size of the object, and one for checking its current element count.

一般来说,我们有两种方法来检查队列的大小–一种是获得对象的最大尺寸,另一种是检查其当前的元素数。

The maxSize method will return an integer value of the queue maximum size:

maxSize方法将返回队列最大尺寸的整数值。

int maxSize = bits.maxSize();

This will return 32, as the bits queue was created with the default constructor.

这将返回32,因为bits队列是用默认构造函数创建的。

The size method will return the number of elements currently stored in the queue:

size方法将返回当前存储在队列中的元素数量。

int size = colors.size();

To check the capacity of the queue object, we can use the isEmpty and isAtFullCapacity methods.

为了检查队列对象的容量,我们可以使用isEmptyisAtFullCapacity方法。

The isEmpty method will return a boolean value that indicates whether the queue is empty or not:

isEmpty方法将返回一个boolean值,表明队列是否为空。

boolean isEmpty = bits.isEmpty();

To check if our queue is full, we can use the isAtFullCapacity method. This method returns true only if the maximum size of elements in the queue has been reached:

为了检查我们的队列是否已满,我们可以使用isAtFullCapacity方法。这个方法只在队列中的元素达到最大尺寸时返回true

boolean isFull = daysOfWeek.isAtFullCapacity();

You should note that this method is available as of version 4.1.

你应该注意,这个方法从4.1版本开始可用

Another method of the Queue interface that we can use to check whether our queue is full is the isFull method. For CircularFifoQueue, the isFull method will always return false, because the queue can always accept new elements:

Queue接口的另一个方法是isFull方法,我们可以用它来检查我们的队列是否已满。对于CircularFifoQueueisFull方法将总是返回false,因为队列总是可以接受新元素

boolean isFull = daysOfWeek.isFull();

7. Conclusion

7.结论

In this article, we saw how to use the Apache Commons CircularFifoQueue. We saw some examples that illustrate how to instantiate a queue object, how to fill it, how to empty it, how to get and remove elements from it, and how to check its size and capacity.

在这篇文章中,我们看到了如何使用Apache Commons的CircularFifoQueue。我们看到了一些例子,说明了如何实例化一个队列对象,如何填充它,如何清空它,如何获取和移除其中的元素,以及如何检查其大小和容量。

You can find the full example code used in this article in our GitHub project. This is a Maven project, so you should be able to import it and run it as it is.

你可以在我们的GitHub项目中找到本文使用的完整示例代码。这是一个Maven项目,所以你应该可以导入它并按原样运行。

« Previous

Apache Commons Collections MapUtils