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 Deque
giao 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 Queue
giao 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 Deque
giao 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.Deque
gó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 Deque
giao diện.
Phương pháp Deque
Vì Deque
mở rộng Queue
giao 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 Queue
giao diện, Deque
giao 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 Stack
lớp của Java Collections
framework 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 Deque
như 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à Deque
giao diện cung cấp để triển khai ngăn xếp:
push()
– adds an element at the beginning of dequepop()
– removes an element from the beginning of dequepeek()
– 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 .