Home  >  Article  >  Java  >  How to use Java Queue queue

How to use Java Queue queue

小老鼠
小老鼠Original
2023-12-26 17:09:191297browse

In Java, the Queue interface represents a queue data structure that follows the first-in-first-out (FIFO) principle, that is, the elements that are put into the queue first are dequeued first. Its usage: LinkedList is used as the implementation of Queue. Elements are added to the queue, then the head element is removed through the poll method, the head element is obtained through the peek method without removal, and finally the elements in the queue are traversed through iteration.

How to use Java Queue queue

Operating system for this tutorial: Windows 10 system, Dell G3 computer.

In Java, the Queue interface represents a queue data structure that follows the first-in-first-out (FIFO) principle, that is, the elements that are put into the queue first are dequeued first. The Queue interface inherits from the Collection interface, which defines some methods for operating queues. The Queue interface has two main implementation classes: LinkedList and PriorityQueue.

The following are some commonly used Queue interface methods:

1. add(E e) / offer(E e): Insert the specified element into the queue. The add method will throw an exception when the queue is full, and the offer method will return a special value (such as true or false) to indicate whether the insertion was successful.

Queue<String> queue = new LinkedList<>();
queue.add("Element 1");
queue.offer("Element 2");

2. remove() / poll(): Remove and return the head element of the queue. The remove method will throw an exception when the queue is empty, and the poll method will return a special value (such as null) to indicate whether the removal was successful.

String element = queue.remove();
String elementOrNull = queue.poll();

3. element() / peek(): Returns the head element of the queue, but does not remove it. The element method will throw an exception when the queue is empty, and the peek method will return a special value (such as null) to indicate whether the acquisition is successful.

String peekedElement = queue.element();
String peekedElementOrNull = queue.peek();

4. size(): Returns the number of elements in the queue.

int size = queue.size();

5. isEmpty(): Determine whether the queue is empty.

boolean isEmpty = queue.isEmpty();

6. clear(): Clear all elements in the queue.

queue.clear();

7. Other methods: In addition to the above basic methods, the Queue interface also defines some other methods, such as addAll, removeAll, retainAll, etc., to support collection operations.

The following is a simple example that demonstrates how to use the Queue interface:

import java.util.LinkedList;
import java.util.Queue;
public class QueueExample {
    public static void main(String[] args) {
        Queue<String> queue = new LinkedList<>();
        // 添加元素
        queue.offer("Element 1");
        queue.offer("Element 2");
        queue.offer("Element 3");
        // 获取并移除头部元素
        String removedElement = queue.poll();
        System.out.println("Removed Element: " + removedElement);
        // 获取头部元素但不移除
        String peekedElement = queue.peek();
        System.out.println("Peeked Element: " + peekedElement);
        // 遍历队列
        System.out.println("Queue Elements:");
        for (String element : queue) {
            System.out.println(element);
        }
    }
}

In this example, LinkedList is used as the implementation of Queue, elements are added to the queue, and then polled method to remove the head element, obtain the head element through the peek method without removing it, and finally iterate through the elements in the queue.

The above is the detailed content of How to use Java Queue queue. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn