Giao diện hàng đợi Java

Trong hướng dẫn này, chúng ta sẽ tìm hiểu về giao diện Java Queue và các phương thức của nó.

Các Queuegiao diện của khuôn khổ bộ sưu tập Java cung cấp các chức năng của cấu trúc dữ liệu hàng đợi. Nó mở rộng Collectiongiao diện.

Các lớp triển khai hàng đợi

Vì Queuelà một giao diện, chúng tôi không thể cung cấp việc triển khai trực tiếp nó.

Để sử dụng các chức năng của Queue, chúng ta cần sử dụng các lớp triển khai nó:

  • ArrayDeque
  • LinkedList
  • PriorityQueue

Các giao diện mở rộng hàng đợi

Các Queuegiao diện cũng được mở rộng bởi subinterface khác nhau:

  • Deque
  • BlockingQueue
  • BlockingDeque

Hoạt động của cấu trúc dữ liệu hàng đợi

Trong hàng đợi, các phần tử được lưu trữ và truy cập theo cách First In, First Out . Có nghĩa là, các phần tử được thêm vào từ phía sau và loại bỏ từ phía trước .

Làm thế nào để sử dụng Hàng đợi?

Trong Java, chúng ta phải nhập java.util.Queuegói để sử dụng Queue.

// LinkedList implementation of Queue
Queue<String> animal1 = new LinkedList<>();

// Array implementation of Queue
Queue<String> animal2 = new ArrayDeque<>();

// Priority Queue implementation of Queue
Queue<String> animal 3 = new PriorityQueue<>();

Ở đây, chúng tôi đã tạo các đối tượng thú1 , thú2 và thú3 của các lớp LinkedListArrayDequevà PriorityQueuetương ứng. Các đối tượng này có thể sử dụng các chức năng của Queuegiao diện.

Phương thức xếp hàng

Các Queuegiao diện bao gồm tất cả các phương pháp của Collectiongiao diện. Đó là vì Collectionlà siêu giao diện của Queue.

Một số phương pháp thường được sử dụng của Queuegiao diện là:

  • add() – Inserts the specified element into the queue. If the task is successful, add() returns true, if not it throws an exception.
  • offer() – Inserts the specified element into the queue. If the task is successful, offer() returns true, if not it returns false.
  • element() – Returns the head of the queue. Throws an exception if the queue is empty.
  • peek() – Returns the head of the queue. Returns null if the queue is empty.
  • remove() – Returns and removes the head of the queue. Throws an exception if the queue is empty.
  • poll() – Returns and removes the head of the queue. Returns null if the queue is empty.

Triển khai giao diện hàng đợi

1. Triển khai lớp LinkedList

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

class Main {

    public static void main(String[] args) {
        // Creating Queue using the LinkedList class
        Queue<Integer> numbers = new LinkedList<>();

        // offer elements to the Queue
        numbers.offer(1);
        numbers.offer(2);
        numbers.offer(3);
        System.out.println("Queue: " + numbers);

        // Access elements of the Queue
        int accessedNumber = numbers.peek();
        System.out.println("Accessed Element: " + accessedNumber);

        // Remove elements from the Queue
        int removedNumber = numbers.poll();
        System.out.println("Removed Element: " + removedNumber);

        System.out.println("Updated Queue: " + numbers);
    }
}

Đầu ra

Queue: [1, 2, 3]
Accessed Element: 1
Removed Element: 1
Updated Queue: [2, 3]

Để tìm hiểu thêm, hãy truy cập Java LinkedList .

2. Triển khai lớp PriorityQueue

import java.util.Queue;
import java.util.PriorityQueue;

class Main {

    public static void main(String[] args) {
        // Creating Queue using the PriorityQueue class
        Queue<Integer> numbers = new PriorityQueue<>();

        // offer elements to the Queue
        numbers.offer(5);
        numbers.offer(1);
        numbers.offer(2);
        System.out.println("Queue: " + numbers);

        // Access elements of the Queue
        int accessedNumber = numbers.peek();
        System.out.println("Accessed Element: " + accessedNumber);

        // Remove elements from the Queue
        int removedNumber = numbers.poll();
        System.out.println("Removed Element: " + removedNumber);

        System.out.println("Updated Queue: " + numbers);
    }
}

Đầu ra

Queue: [1, 5, 2]
Accessed Element: 1
Removed Element: 1
Updated Queue: [2, 5]

Để tìm hiểu thêm, hãy truy cập Java PriorityQueue .

Trong các hướng dẫn tiếp theo, chúng ta sẽ tìm hiểu về các Queuegiao diện con khác nhau của giao diện và cách triển khai của nó một cách chi tiết.









Gõ tìm kiếm nhanh...