Detailed Java-Queue Queue (Deque / PriorityQueue / Deque / ArrayDeque / LinkedList)

Queue

Queue Queue Introduction

  Queue is used to simulate the queue, the queue Shajiao? Queue is the queue means, such as checkout line, into the first team, first routed to pay the bill and leave; back row to enter the team, and other people in front of the team after the team talk in the back pay. In line with “先进先出FIFO”the rules, it is a linear table. Is inserted at one end, the other end is deleted. Enqueue ( offer) in the tail, dequeue ( poll) in the first team. There Queue interface implementation class PriorityQueue, another deque interfaces Deque.

Queue Queue schematic out

Queue schematic out

Queue common method

  PriorityQueue owns Queueand Collectionmethods. The following describes some of the commonly used methods:

  • boolean add(E e);: Add the specified element to the tail of the queue.
  • E element();: Gets the element head of the queue, but does not remove the element.
  • boolean offer(E e);: Add the specified element to the tail of the queue.
  • E peek();: Gets the element head of the queue, but does not remove elements; if the queue is empty, returns null.
  • E poll();: Gets the element head of the queue, and remove the element. If the queue is empty, return null.
  • E remove();: Gets the element head of the queue, and remove the element. If the queue is empty, throw an exception.

PriorityQueue

PriorityQueue Overview

  PriorityQueue queue Queue is the implementation class, storage order PriorityQueue queue elements are not added to the queue in the order, but re-sorted by size queue element. When calling peek(), or poll()when the queue element method of acquiring, acquiring the smallest element of the queue, rather than the first in queue element. A little 反先入先出规则.

Examples PriorityQueue

1) run the main categories:

public class DemoApplication {

    public static void main(String[] args) {

        PriorityQueue priorityQueue = new PriorityQueue();
        priorityQueue.offer(5);
        priorityQueue.offer(-1);
        priorityQueue.offer(3);
        priorityQueue.offer(7);

        //查看入队顺序
        System.out.println("队列输出:" + priorityQueue);

        //peek方法获取队头元素但是不删除元素
        System.out.println("peek()方法获取队头:" + priorityQueue.peek());

        //查看第一个元素即为最小元素
        System.out.println("第一个队列元素出队:" + priorityQueue.poll());
        System.out.println("第二个队列元素出队:" + priorityQueue.poll());
        System.out.println("第三个队列元素出队:" + priorityQueue.poll());
        System.out.println("第四个队列元素出队:" + priorityQueue.poll());
        System.out.println("null队列:" + priorityQueue.poll());
        
    }

}
复制代码

2) Run results:

队列输出:[-1, 5, 3, 7]
peek()方法获取队头:-1
第一个队列元素出队:-1
第二个队列元素出队:3
第三个队列元素出队:5
第四个队列元素出队:7
null队列:null
复制代码

and

Introduction Deque deque

  Deque is the Queue sub-interface is 双端队列. Can be added simultaneously from both ends (head and tail of the queue), remove elements. It can be used to implement the stack data structure. There are two classes implement ( ArrayDequeand LinkedList)

Deque common method

  • void addFirst(E e): The insertion head of the specified element deque, more important, many methods below are achieved by a head portion inserted into the interior to achieve this, such as offerFirst ().
  • void addLast(E e): Inserts the specified element to the tail of the deque. More important, the following is added to the end inserted into the interior to achieve this is achieved by, as offerLast ().
  • E getFirst(): Retrieves, but does not remove the double-ended queue of the first element.
  • E getLast(): Retrieves, but does not remove the double-ended queue of the last element.
  • E removeFirst(): Obtain and removes the first element of the double-ended queue. The method of the queue stack pop () is achieved by the method.
  • E removeLast(): Obtain and remove the last element of the double-ended queue.
  • Iterator<E> descendingIterator(): Returns the deque iterator, iterative element in reverse order.
  • boolean offerFirst(E e): The insertion head of the specified element deque.
    public boolean offerFirst(E e) {
        addFirst(e);
        return true;
    }
复制代码
  • boolean offerLast(E e): Inserts the specified element to the tail of the deque.
    public boolean offerLast(E e) {
        addLast(e);
        return true;
    }
复制代码
  • E peekFirst(): Get the first element but does not delete the deque; if the queue is empty, or null.
    public E peekFirst() {
        final Node<E> f = first;
        return (f == null) ? null : f.item;
     }
复制代码
  • E peekLast(): Get but does not delete the deque last element; if the queue is empty, or null.
    public E peekLast() {
        final Node<E> l = last;
        return (l == null) ? null : l.item;
    }
复制代码
  • E pollFirst(): Get and remove the first element of the double-ended queue; if the queue is empty, null is returned.
    public E pollFirst() {
        final Node<E> f = first;
        return (f == null) ? null : unlinkFirst(f);
    }
复制代码
  • E pollLast(): Get and remove the last element of the double-ended queue; if the queue is empty, null is returned.
    public E pollLast() {
        final Node<E> l = last;
        return (l == null) ? null : unlinkLast(l);
    }
复制代码
  • E pop(): Method stack, pop out of the deque top element of the stack represents the equivalent to removeFirst () method.
    public E pop() {
        return removeFirst();
    }
复制代码
  • void push(E e): Stack method, Push element enters the top of the stack represents the deque stack, equivalent to addFist () method.
    public void push(E e) {
        addFirst(e);
    }
复制代码
  • boolean removeFirstOccurrence(Object o): Deque remove the first occurrence of the elements o, the underlying method is implemented by remove ().
    public boolean removeFirstOccurrence(Object o) {
        return remove(o);
    }
复制代码
  • boolean removeLastOccurrence(Object o): Remove the short rinse queue element o the last occurrence.

ArrayDeque

ArrayDeque Overview

  ArrayDeque underlying implementation is similar to the ArrayList, are dynamic, can be assigned by the Object [] arrays to implement the storage element, when the collection element exceeds the capacity of the array, it will be reallocated to store a new set of array elements.

Examples ArrayDeque

1) Run Main

public class DemoApplication {
    public static void main(String[] args) {
        //可以作为栈来使用,先进后出
        ArrayDeque<String> arrayDeque = new ArrayDeque<>();
        arrayDeque.push("book01");
        arrayDeque.push("book03");
        arrayDeque.push("book02");
        arrayDeque.push("book04");

        System.out.println("原栈:" + arrayDeque);

        System.out.println("获取头部元素,但不删除该元素,peek(): " + arrayDeque.peek());
        System.out.println("获取头部元素,且删除该元素,pop(): " +arrayDeque.pop());
        System.out.println("获取第一个元素,但不删除:" + arrayDeque.getFirst());
        System.out.println("获取最后一个元素,但不删除:" + arrayDeque.getLast());

        System.out.println("在双端队列头部插入元素:" + arrayDeque.offerFirst("booknew01"));
        System.out.println("在双端队列尾部插入元素:" + arrayDeque.offerLast("booknew02"));
        System.out.println("新栈:" + arrayDeque);

    }

}
复制代码

2) Run results

原栈:[book04, book02, book03, book01]
获取头部元素,但不删除该元素: book04
获取头部元素,且删除该元素: book04
获取第一个元素,但不删除:book02
获取最后一个元素,但不删除:book01
true
true
新栈:[booknew01, book02, book03, book01, booknew02]
复制代码

LinkedList

LinkedList Overview

  List LinkedList achieve, but also to achieve the Deque, can be used as deque, it can be used as "stacks" or "queues" use. And the ArrayList LinkedList, except that the underlying implementation ArrayDeque, LinkedList underlying elements of the list is stored in the form of 随机访问性能比较差, but 插入, 删除when 性能比较好(only need to change the address pointer on line)

Q&A

Similarities and differences peek () and element () of

  • With: peek () and element () are the first team to return without removal;
  • Iso: peek () method returns null in the queue is empty, the element () will throw an exception NoSuchElementException.

Similarities and differences poll () and remove () of

  • With: poll () and remove () Removes and returns all the teams head;
  • Different: poll () returns null in the queue is empty, and remove () will throw an exception NoSuchElementException.

Queue and stack difference?

queue

  1. Queue in accordance with 先进先出FIFOthe operation principle of the element.
  2. All queues are defined only at one end is inserted into the table is, a linear table is deleted at the other end of the table.
  3. Table allowed the insertion of the tail end, allowing the team to remove the head end.

Stack 4 Stack is a special linear form, is a 后进先出LIFOstructure; 5. stack is the first end of the table given only insertion and deletion operations linear table, that is the end of the table 栈顶, the first table called bottom of the stack. 6. The stack may be familiar with the physical storage sequential storage structure, may be stored with the chain structure.

Guess you like

Origin juejin.im/post/5e8c4293e51d454719460e3a