Remove Elements From a Queue Using Loop – 使用循环从队列中移除元素

最后修改: 2023年 10月 26日

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

1. Introduction

1.导言

The first-in-first-out (FIFO) principle governs a basic data structure in computer science called the queue. Moreover, the queuing process involves appending the items onto the tail and removing them at the head, which is provided in the Queue interface in Java.

先入先出(FIFO)原则支配着计算机科学中一种名为队列的基本数据结构。此外,队列过程包括在尾部追加项目和在头部删除项目,Java 中的 Queue 接口提供了这一过程。

Nevertheless, some contexts will require deleting a few elements from a queue depending on certain terms.

不过,在某些情况下,需要根据某些条件从队列中删除一些元素。

In this tutorial, we’ll discuss deleting elements from the queue using loops in Java.

在本教程中,我们将讨论如何使用 Java 中的循环从队列中删除元素。

2. Removing Even Numbers from a Queue

2.从队列中删除偶数

While deleting elements, deletion may be made on the basis of custom conditions from the queue. If it is a queue of integers that contains only even values, let’s try to eradicate them. To do so, we’ll iterate through the queue with a loop and delete all the even numbers as follows:

在删除元素时,可以根据队列中的自定义条件进行删除。如果这是一个只包含偶数值的整数队列,让我们尝试删除它们。为此,我们将用一个循环遍历队列,并删除所有偶数,如下所示:

@Test
public void givenQueueWithEvenAndOddNumbers_whenRemovingEvenNumbers_thenOddNumbersRemain() {
    Queue<Integer> queue = new LinkedList<>();
    queue.add(1);
    queue.add(2);
    queue.add(3);
    queue.add(4);
    queue.add(5);

    Queue<Integer> oddElementsQueue = new LinkedList<>();

    while (queue.peek() != null) {
        int element = queue.remove();
        if (element % 2 != 0) {
            oddElementsQueue.add(element);
        }
    }

    assertEquals(3, oddElementsQueue.size());
    assertTrue(oddElementsQueue.contains(1));
    assertTrue(oddElementsQueue.contains(3));
    assertTrue(oddElementsQueue.contains(5));
}

In the above test method, we start by initializing a new queue with numbers (1, 2, 3, 4, and 5) and an empty queue named oddElementsQueue. Additionally, we use a while loop to remove elements from the queue one by one until it becomes empty. If the element is odd, it is added to the oddElementsQueue. Finally, the test asserts that the oddElementsQueue contains three elements, specifically the odd numbers 1, 3, and 5 using the assertTrue method.

在上述测试方法中,我们首先用数字(1、2、3、4 和 5)初始化一个新的 queue 和一个名为 oddElementsQueue 的空 queue此外,我们还使用 while 循环从 queue 中逐个删除元素,直至其为空。 最后,测试 asserts 使用 assertTrue 方法确认 oddElementsQueue 包含三个元素,特别是奇数 1、3 和 5。

3. Removing Strings That Begin With a Particular Letter

3.删除以特定字母开头的字符串

Suppose we have a queue of strings that we would like to delete the strings that begin with a particular letter. Let’s take the following example:

假设我们有一个字符串队列,我们希望删除以特定字母开头的字符串。让我们以下面的示例为例:

@Test
public void givenStringQueue_whenRemovingStringsThatStartWithA_thenStringElementsRemain() {
    Queue<String> queue = new LinkedList<>();
    queue.add("Apple");
    queue.add("Banana");
    queue.add("Orange");
    queue.add("Grape");
    queue.add("Mango");

    Queue<String> stringElementsQueue = new LinkedList<>();

    while (queue.peek() != null) {
        String element = queue.remove();
        if (!element.startsWith("A")) {
            stringElementsQueue.add(element);
        }
    }

    assertEquals(4, stringElementsQueue.size());
    assertTrue(stringElementsQueue.contains("Banana"));
    assertTrue(stringElementsQueue.contains("Orange"));
    assertTrue(stringElementsQueue.contains("Grape"));
    assertTrue(stringElementsQueue.contains("Mango"));
}

In the above code, we iterate through the elements of the queue one by one until it becomes empty. We also check if this element doesn’t start with the letter “A”, it is added to the stringElementsQueue queue. Finally, we utilize asserts to verify that the stringElementsQueue contains four elements, specifically the strings (“Apple”, “Banana”, “Orange”, “Grape”, and “Mango”) using the assertTrue method.

在上述代码中,我们逐个遍历 queue 中的元素,直到其为空。我们还会检查该元素是否不是以字母 “A” 开头,并将其添加到 stringElementsQueue 队列中。最后,我们使用 asserts 方法来验证 stringElementsQueue 是否包含四个元素,特别是字符串(“Apple”“Banana”“Orange”“Grape”“Mango”)

4. Conclusion

4.结论

In this tutorial, we looked into removing elements from a queue through a while loop in Java. We discussed two scenarios: how to take out even numbers in the queue and another situation whereby one can pick out strings that start with a specific letter.

在本教程中,我们研究了如何通过 Java 中的 while 循环从队列中移除元素。我们讨论了两种情况:如何从队列中移除偶数,以及另一种情况,即可以移除以特定字母开头的字符串。

Learning how to operate queues is very important in Java because it forms part of your overall programming knowledge.

在 Java 中,学习如何操作队列是非常重要的,因为它是整个编程知识的一部分。

As always, the complete code samples for this article can be found over on GitHub.

与往常一样,本文的完整代码示例可在 GitHub 上找到