美文网首页
Deque Java 源码

Deque Java 源码

作者: huxq_coder | 来源:发表于2020-08-20 14:19 被阅读0次

    Deque 双端队列(Double Ended Queue)
    允许两头都进、都出的队列。特性是 既可以添加到队首,也可以添加到队尾;既可以从队头获取,也可以从队尾获取。
    Java中的双端队列是一个接口(Interface),它的实现类有ArrayDeque、LinkedList等。
    文档:https://docs.oracle.com/javase/10/docs/api/java/util/Deque.html

    接口方法

    返回值 方法名 描述
    boolean add(E e) Inserts the specified element into the queue represented by this deque (in other words, at the tail of this deque) if it is possible to do so immediately without violating capacity restrictions, returning true upon success and throwing an IllegalStateException if no space is currently available.
    boolean addAll​(Collection<? extends E> c) Adds all of the elements in the specified collection at the end of this deque, as if by calling addLast(E) on each one, in the order that they are returned by the collection's iterator.
    void addFirst​(E e) Inserts the specified element at the front of this deque if it is possible to do so immediately without violating capacity restrictions, throwing an IllegalStateException if no space is currently available.
    void addLast​(E e) Inserts the specified element at the end of this deque if it is possible to do so immediately without violating capacity restrictions, throwing an IllegalStateException if no space is currently available.
    boolean contains(Object o) Returns true if this deque contains the specified element.
    Iterator<E> descendingIterator() Returns an iterator over the elements in this deque in reverse sequential order.
    E element() Retrieves, but does not remove, the head of the queue represented by this deque (in other words, the first element of this deque).
    E getFirst() Retrieves, but does not remove, the first element of this deque.
    E getLast() Retrieves, but does not remove, the last element of this deque.
    Iterator<E> iterator Returns an iterator over the elements in this deque in proper sequence.
    boolean offer​(E e) Inserts the specified element into the queue represented by this deque (in other words, at the tail of this deque) if it is possible to do so immediately without violating capacity restrictions, returning true upon success and false if no space is currently available.
    boolean offerFirst​(E e) Inserts the specified element at the front of this deque unless it would violate capacity restrictions.
    boolean offerLast​(E e) Inserts the specified element at the end of this deque unless it would violate capacity restrictions.
    E peek() Retrieves, but does not remove, the head of the queue represented by this deque (in other words, the first element of this deque), or returns null if this deque is empty.
    E peekFirs() Retrieves, but does not remove, the first element of this deque, or returns null if this deque is empty.
    E peekLast() Retrieves, but does not remove, the last element of this deque, or returns null if this deque is empty.
    E pol() Retrieves and removes the head of the queue represented by this deque (in other words, the first element of this deque), or returns null if this deque is empty.
    E pollFirst() Retrieves and removes the first element of this deque, or returns null if this deque is empty.
    E pollLast() Retrieves and removes the last element of this deque, or returns null if this deque is empty.
    E pop() Pops an element from the stack represented by this deque.
    void push​(E e) Pushes an element onto the stack represented by this deque (in other words, at the head of this deque) if it is possible to do so immediately without violating capacity restrictions, throwing an IllegalStateException if no space is currently available.
    E remove() Retrieves and removes the head of the queue represented by this deque (in other words, the first element of this deque).
    boolean remove​(Object o) Removes the first occurrence of the specified element from this deque.
    E removeFirst() Retrieves and removes the first element of this deque.
    boolean removeFirstOccurrence​(Object o) Removes the first occurrence of the specified element from this deque.
    E removeLast() Retrieves and removes the last element of this deque.
    boolean removeLastOccurrence​(Object o) Removes the last occurrence of the specified element from this deque.
    int size() Returns the number of elements in this deque.
    import java.util.Deque;
    import java.util.LinkedList;
    
    public class DequeDemo{
        public static void main(String[] args) {
            Deque<String> deque = new LinkedList<>();
    
            // 添加
            System.out.println("offerLast()  " + deque.offerLast("A"));
            System.out.println("offerLast()  " + deque.offerLast("B")); 
            System.out.println("offerFirst()  " + deque.offerFirst("C")); 
            System.out.println(deque);
            deque.addFirst("E"); 
            deque.addFirst("F"); 
            deque.addLast("G"); 
            System.out.println(deque);
            
            System.out.println("getFirst() " + deque.getFirst());
            System.out.println("getLast() " + deque.getLast());
                
            System.out.println("pollFirst()  " + deque.pollFirst());
            System.out.println("pollLast()  " + deque.pollLast());
            System.out.println(deque);
            System.out.println("peekFirst()  " + deque.peekFirst());
            System.out.println("peekLast()  " + deque.peekLast());
            System.out.println("pop()  " + deque.pop());
            
            System.out.println(deque);
            System.out.println("removeFirst()  " + deque.removeFirst());
            System.out.println("removeLast()  " + deque.removeLast());
            
            System.out.println("pollFirst()  " + deque.pollFirst());
            System.out.println("pollFirst()  " + deque.pollFirst());
            try{
                System.out.println("removeFirst()  " + deque.removeFirst());
                System.out.println("removeLast()  " + deque.removeLast());
            } catch (Exception e) {
                System.out.println("exception " + e);
            }
            System.out.println("removeLast()  " + deque.poll());
        }
    }
    

    相关文章

      网友评论

          本文标题:Deque Java 源码

          本文链接:https://www.haomeiwen.com/subject/ppbdjktx.html