gpt4 book ai didi

Java Queue队列接口

转载 作者:知者 更新时间:2024-03-11 23:51:17 28 4
gpt4 key购买 nike

队列是先进先出 (FIFO) 数据结构。它模拟了现实生活中的队列。是的,您可能在电影院、购物中心、地铁或公共汽车前看到过的那种。

就像现实生活中的队列一样,队列数据结构中的新元素在后面添加并从前面删除。可以将队列可视化,如下图所示。

在Queue后面添加一个元素的过程称为Enqueue,从Queue前面移除一个元素的过程称为Dequeue。

Java 提供了一个 Queue 接口,它是 Java 集合框架的一部分。下图描述了 Queue 接口在 Collections 层次结构中的位置

Java中的队列只是一个接口。我们需要在我们的程序中使用 Queue 接口的具体实现。

如上图所示,LinkedList 类实现了 Queue 接口,因此可以用作 Queue。

创建队列并执行基本操作,如入队和出队

import java.util.LinkedList;
import java.util.Queue;

public class QueueExample {
    public static void main(String[] args) {
        // Create and initialize a Queue using a LinkedList
        Queue<String> waitingQueue = new LinkedList<>();

        // Adding new elements to the Queue (The Enqueue operation)
        waitingQueue.add("Rajeev");
        waitingQueue.add("Chris");
        waitingQueue.add("John");
        waitingQueue.add("Mark");
        waitingQueue.add("Steven");

        System.out.println("WaitingQueue : " + waitingQueue);

        // Removing an element from the Queue using remove() (The Dequeue operation)
        // The remove() method throws NoSuchElementException if the Queue is empty
        String name = waitingQueue.remove();
        System.out.println("Removed from WaitingQueue : " + name + " | New WaitingQueue : " + waitingQueue);

        // Removing an element from the Queue using poll()
        // The poll() method is similar to remove() except that it returns null if the Queue is empty.
        name = waitingQueue.poll();
        System.out.println("Removed from WaitingQueue : " + name + " | New WaitingQueue : " + waitingQueue);
    }
}
# Output
WaitingQueue : [Rajeev, Chris, John, Mark, Steven]
Removed from WaitingQueue : Rajeev | New WaitingQueue : [Chris, John, Mark, Steven]
Removed from WaitingQueue : Chris | New WaitingQueue : [John, Mark, Steven]

查看队列内部

  • 检查队列是否为空。
  • 查找队列的大小。
  • 在队列中搜索元素。
  • 获取队列前面的元素而不移除它。
import java.util.LinkedList;
import java.util.Queue;

public class QueueSizeSearchFrontExample {
    public static void main(String[] args) {
        Queue<String> waitingQueue = new LinkedList<>();

        waitingQueue.add("Jennifer");
        waitingQueue.add("Angelina");
        waitingQueue.add("Johnny");
        waitingQueue.add("Sachin");

        System.out.println("WaitingQueue : " + waitingQueue);

        // Check is a Queue is empty
        System.out.println("is waitingQueue empty? : " + waitingQueue.isEmpty());

        // Find the size of the Queue
        System.out.println("Size of waitingQueue : " + waitingQueue.size());

        // Check if the Queue contains an element
        String name = "Johnny";
        if(waitingQueue.contains(name)) {
            System.out.println("WaitingQueue contains " + name);
        } else {
            System.out.println("Waiting Queue doesn't contain " + name);
        }

        // Get the element at the front of the Queue without removing it using element()
        // The element() method throws NoSuchElementException if the Queue is empty
        String firstPersonInTheWaitingQueue =  waitingQueue.element();
        System.out.println("First Person in the Waiting Queue (element()) : " + firstPersonInTheWaitingQueue);

        // Get the element at the front of the Queue without removing it using peek()
        // The peek() method is similar to element() except that it returns null if the Queue is empty
        firstPersonInTheWaitingQueue = waitingQueue.peek();
        System.out.println("First Person in the Waiting Queue : " + firstPersonInTheWaitingQueue);

    }
}
# Output
WaitingQueue : [Jennifer, Angelina, Johnny, Sachin]
is waitingQueue empty? : false
Size of waitingQueue : 4
WaitingQueue contains Johnny
First Person in the Waiting Queue (element()) : Jennifer
First Person in the Waiting Queue : Jennifer

遍历 Java 中的队列

本节中的示例显示了迭代队列的各种方法:

  • 使用 Java 8 forEach() 方法遍历队列。
  • 使用 iterator() 遍历队列。
  • 使用 iterator() 和 Java 8 forEachRemaining() 方法遍历队列。
  • 使用简单的 for-each 循环遍历队列。

队列中的迭代顺序与插入顺序相同。

import java.util.Iterator;
import java.util.LinkedList;
import java.util.Queue;

public class IterateOverQueueExample {
    public static void main(String[] args) {
        Queue<String> waitingQueue = new LinkedList<>();

        waitingQueue.add("John");
        waitingQueue.add("Brad");
        waitingQueue.add("Angelina");
        waitingQueue.add("Julia");

        System.out.println("=== Iterating over a Queue using Java 8 forEach() ===");
        waitingQueue.forEach(name -> {
            System.out.println(name);
        });

        System.out.println("\n=== Iterating over a Queue using iterator() ===");
        Iterator<String> waitingQueueIterator = waitingQueue.iterator();
        while (waitingQueueIterator.hasNext()) {
            String name = waitingQueueIterator.next();
            System.out.println(name);
        }

        System.out.println("\n=== Iterating over a Queue using iterator() and Java 8 forEachRemaining() ===");
        waitingQueueIterator = waitingQueue.iterator();
        waitingQueueIterator.forEachRemaining(name -> {
            System.out.println(name);
        });

        System.out.println("\n=== Iterating over a Queue using simple for-each loop ===");
        for(String name: waitingQueue) {
            System.out.println(name);
        }
    }
}
# Output
=== Iterating over a Queue using Java 8 forEach() ===
John
Brad
Angelina
Julia

=== Iterating over a Queue using iterator() ===
John
Brad
Angelina
Julia

=== Iterating over a Queue using iterator() and Java 8 forEachRemaining() ===
John
Brad
Angelina
Julia

=== Iterating over a Queue using simple for-each loop ===
John
Brad
Angelina
Julia

结论

这就是所有人!在本文中,您了解了什么是 Queue 数据结构,如何在 Java 中创建 Queue,如何向 Queue 添加新元素,如何从 Queue 中删除元素,以及如何在 Queue 中搜索元素。

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com