Giao diện Java Deque

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

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

Làm việc của Deque

Trong một hàng đợi thông thường, các phần tử được thêm vào từ phía sau và loại bỏ từ phía trước. Tuy nhiên, trong deque, chúng ta có thể chèn và xóa các phần tử từ cả phía trước và phía sau .

Các lớp triển khai Deque

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

  • ArrayDeque
  • LinkedList

Làm thế nào để sử dụng Deque?

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

// Array implementation of Deque
Deque<String> animal1 = new ArrayDeque<>();

// LinkedList implementation of Deque
Deque<String> animal2 = new LinkedList<>();

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

Phương pháp Deque

Vì Dequemở rộng Queuegiao diện, nó kế thừa tất cả các phương thức của giao diện Hàng đợi .

Bên cạnh các phương thức có sẵn trong Queuegiao diện, Dequegiao diện còn bao gồm các phương thức sau:

  • addFirst() – Adds the specified element at the beginning of the deque. Throws an exception if the deque is full.
  • addLast() – Adds the specified element at the end of the deque. Throws an exception if the deque is full.
  • offerFirst() – Adds the specified element at the beginning of the deque. Returns false if the deque is full.
  • offerLast() – Adds the specified element at the end of the deque. Returns false if the deque is full.
  • getFirst() – Returns the first element of the deque. Throws an exception if the deque is empty.
  • getLast() – Returns the last element of the deque. Throws an exception if the deque is empty.
  • peekFirst() – Returns the first element of the deque. Returns null if the deque is empty.
  • peekLast() – Returns the last element of the deque. Returns null if the deque is empty.
  • removeFirst() – Returns and removes the first element of the deque. Throws an exception if the deque is empty.
  • removeLast() – Returns and removes the last element of the deque. Throws an exception if the deque is empty.
  • pollFirst() – Returns and removes the first element of the deque. Returns null if the deque is empty.
  • pollLast() – Returns and removes the last element of the deque. Returns null if the deque is empty.

Deque as Stack Data Structure

Các Stacklớp của Java Collectionsframework cung cấp việc thực hiện của ngăn xếp.

Tuy nhiên, nó được khuyến khích sử dụng Dequenhư một ngăn xếp thay vì lớp Ngăn xếp . Đó là bởi vì các phương thức của Stackđược đồng bộ hóa.

Dưới đây là các phương pháp mà Dequegiao diện cung cấp để triển khai ngăn xếp:

  • push() – adds an element at the beginning of deque
  • pop() – removes an element from the beginning of deque
  • peek() – returns an element from the beginning of deque

Triển khai Deque trong ArrayDeque Class

import java.util.Deque;
import java.util.ArrayDeque;

class Main {

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

        // add elements to the Deque
        numbers.offer(1);
        numbers.offerLast(2);
        numbers.offerFirst(3);
        System.out.println("Deque: " + numbers);

        // Access elements of the Deque
        int firstElement = numbers.peekFirst();
        System.out.println("First Element: " + firstElement);

        int lastElement = numbers.peekLast();
        System.out.println("Last Element: " + lastElement);

        // Remove elements from the Deque
        int removedNumber1 = numbers.pollFirst();
        System.out.println("Removed First Element: " + removedNumber1);

        int removedNumber2 = numbers.pollLast();
        System.out.println("Removed Last Element: " + removedNumber2);

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

Đầu ra

Deque: [3, 1, 2]
First Element: 3
Last Element: 2
Removed First Element: 3
Removed Last Element: 2
Updated Deque: [1]

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









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