📜  Java中的Deque接口与示例

📅  最后修改于: 2022-05-13 01:55:02.181000             🧑  作者: Mango

Java中的Deque接口与示例

Java.util 包中的Deque接口是队列接口的子类型。 Deque 与支持从数据结构的任一端添加或删除元素的双端队列有关。它既可以用作队列(先进先出/FIFO),也可以用作堆栈(后进先出/LIFO)。 Deque 是双端队列的首字母缩写。

Queue-Deque-PriorityQueue-In-Java

声明: deque 接口声明为:

创建双端队列对象

因为 Deque 是一个接口,所以不能创建 deque 类型的对象。我们总是需要一个扩展这个列表的类来创建一个对象。而且,在Java 1.5 中引入泛型之后,可以限制可以存储在 Deque 中的对象类型。这个类型安全的队列可以定义为:

双端队列示例:

// Java program to demonstrate the working
// of a Deque in Java
  
import java.util.*;
  
public class DequeExample {
    public static void main(String[] args)
    {
        Deque deque
            = new LinkedList();
  
        // We can add elements to the queue
        // in various ways
  
        // Add at the last
        deque.add("Element 1 (Tail)");
  
        // Add at the first
        deque.addFirst("Element 2 (Head)");
  
        // Add at the last
        deque.addLast("Element 3 (Tail)");
  
        // Add at the first
        deque.push("Element 4 (Head)");
  
        // Add at the last
        deque.offer("Element 5 (Tail)");
  
        // Add at the first
        deque.offerFirst("Element 6 (Head)");
  
        System.out.println(deque + "\n");
  
        // We can remove the first element
        // or the last element.
        deque.removeFirst();
        deque.removeLast();
        System.out.println("Deque after removing "
                           + "first and last: "
                           + deque);
    }
}
输出:

使用 Deque 接口和 ArrayDeque 类的操作

让我们看看如何使用 ArrayDeque 类对双端队列执行一些常用操作。

1. 添加元素:为了在双端队列中添加元素,我们可以使用 add() 方法。队列和双端队列之间的区别在于,在双端队列中,可以从任何方向进行添加。因此,还有另外两种可用的方法,名为 addFirst() 和 addLast() 用于在任一端添加元素。

// Java program to demonstrate the
// addition of elements in deque
  
import java.util.*;
public class ArrayDequeDemo {
    public static void main(String[] args)
    {
        // Initializing an deque
        Deque dq
            = new ArrayDeque();
  
        // add() method to insert
        dq.add("For");
        dq.addFirst("Geeks");
        dq.addLast("Geeks");
  
        System.out.println(dq);
    }
}
输出:
[Geeks, For, Geeks]

2. 删除元素:为了从双端队列中删除元素,有多种方法可用。由于我们也可以从两端删除,所以 deque 接口为我们提供了removeFirst()removeLast()方法。除此之外,该接口还为我们提供了 poll()、pop()、pollFirst()、pollLast() 方法,其中 pop() 用于删除和返回双端队列的头部。但是,使用 poll() 是因为它提供了与 pop() 相同的功能,并且当双端队列为空时不会返回异常。

// Java program to demonstrate the
// removal of elements in deque
  
import java.util.*;
public class ArrayDequeDemo {
    public static void main(String[] args)
    {
        // Initializing an deque
        Deque dq
            = new ArrayDeque();
  
        // add() method to insert
        dq.add("For");
        dq.addFirst("Geeks");
        dq.addLast("Geeks");
  
        System.out.println(dq);
  
        System.out.println(dq.pop());
  
        System.out.println(dq.poll());
  
        System.out.println(dq.pollFirst());
  
        System.out.println(dq.pollLast());
    }
}
输出:
[Geeks, For, Geeks]
Geeks
For
Geeks
null

3.通过Deque进行迭代:由于一个deque可以从两个方向进行迭代,所以deque接口的iterator方法为我们提供了两种迭代方式。一个从第一个,另一个从后面。

// Java program to demonstrate the
// iteration of elements in deque
  
import java.util.*;
public class ArrayDequeDemo {
    public static void main(String[] args)
    {
        // Initializing an deque
        Deque dq
            = new ArrayDeque();
  
        // add() method to insert
        dq.add("For");
        dq.addFirst("Geeks");
        dq.addLast("Geeks");
        dq.add("is so good");
  
        for (Iterator itr = dq.iterator();
             itr.hasNext();) {
            System.out.print(itr.next() + " ");
        }
  
        System.out.println();
  
        for (Iterator itr = dq.descendingIterator();
             itr.hasNext();) {
            System.out.print(itr.next() + " ");
        }
    }
}
输出:
Geeks For Geeks is so good 
is so good Geeks For Geeks

实现 Deque 接口的类是 ArrayDeque。

ArrayDeque:集合框架中实现的 ArrayDeque 类为我们提供了一种应用 resizable-array 的方法。这是一种特殊的数组,它可以增长并允许用户从队列的两侧添加或删除元素。数组双端队列没有容量限制,它们会根据需要增长以支持使用。它们不是线程安全的,这意味着在没有外部同步的情况下,ArrayDeque 不支持多个线程的并发访问。当用作堆栈时,ArrayDeque 类可能比 Stack 更快。 ArrayDeque 类在用作队列时可能比 LinkedList 更快。让我们看看如何使用这个类创建一个队列对象。

// Java program to demonstrate the
// creation of deque object using the
// ArrayDeque class in Java
  
import java.util.*;
public class ArrayDequeDemo {
    public static void main(String[] args)
    {
        // Initializing an deque
        Deque de_que
            = new ArrayDeque(10);
  
        // add() method to insert
        de_que.add(10);
        de_que.add(20);
        de_que.add(30);
        de_que.add(40);
        de_que.add(50);
  
        System.out.println(de_que);
  
        // clear() method
        de_que.clear();
  
        // addFirst() method to insert the
        // elements at the head
        de_que.addFirst(564);
        de_que.addFirst(291);
  
        // addLast() method to insert the
        // elements at the tail
        de_que.addLast(24);
        de_que.addLast(14);
  
        System.out.println(de_que);
    }
}
输出:
[10, 20, 30, 40, 50]
[291, 564, 24, 14]

Deque 接口的方法

以下是 deque 接口中存在的方法:

MethodDescription
add(element)This method is used to add an element at the tail of the queue. If the Deque is capacity restricted and no space is left for insertion, it returns an IllegalStateException. The function returns true on successful insertion.
addFirst(element)This method is used to add an element at the head of the queue. If the Deque is capacity restricted and no space is left for insertion, it returns an IllegalStateException. The function returns true on successful insertion.
addLast(element)This method is used to add an element at the tail of the queue. If the Deque is capacity restricted and no space is left for insertion, it returns an IllegalStateException. The function returns true on successful insertion.
contains()This method is used to check whether the queue contains the given object or not.
descendingIterator()This method returns an iterator for the deque. The elements will be returned in order from last(tail) to first(head).
element()This method is used to retrieve, but not remove, the head of the queue represented by this deque.
getFirst()This method is used to retrieve, but not remove, the first element of this deque.
getLast()This method is used to retrieve, but not remove, the last element of this deque.
iterator()This method returns an iterator for the deque. The elements will be returned in order from first (head) to last (tail).
offer(element)This method is used to add an element at the tail of the queue. This method is preferable to add() method since this method does not throws an exception when the capacity of the container is full since it returns false.
offerFirst(element)This method is used to add an element at the head of the queue. This method is preferable to addFirst() method since this method does not throws an exception when the capacity of the container is full since it returns false.
offerLast(element)This method is used to add an element at the tail of the queue. This method is preferable to add() method since this method does not throws an exception when the capacity of the container is full since it returns false.
peek()This method is used to retrieve the element at the head of the deque but doesn’t remove the element from the deque. This method returns null if the deque is empty.
peekFirst()This method is used to retrieve the element at the head of the deque but doesn’t remove the element from the deque. This method returns null if the deque is empty.
peekLast()This method is used to retrieve the element at the tail of the deque but doesn’t remove the element from the deque. This method returns null if the deque is empty.
poll()This method is used to retrieve and remove the element at the head of the deque. This method returns null if the deque is empty.
pollFirst()This method is used to retrieve and remove the element at the head of the deque. This method returns null if the deque is empty.
pollLast()This method is used to retrieve and remove the element at the tail of the deque. This method returns null if the deque is empty.
pop()This method is used to remove an element from the head and return it.
push(element)This method is used to add an element at the head of the queue.
removeFirst()This method is used to remove an element from the head of the queue.
removeLast()This method is used to remove an element from the tail of the queue.
size()This method is used to find and return the size of the deque.