Java教程

Java学习笔记补充——Stack、Queue学习笔记

本文主要是介绍Java学习笔记补充——Stack、Queue学习笔记,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

Stack

栈是继承自List接口的。因此,list接口有的方法,它基本都有。因此,本人主要补充Stack的一些自己实现的方法:
1.push()

public E push(E item)

2.pop()

public E pop()

3.peek()

public E peek()

4.empty()

public boolean empty()

5.search()
返回对象在堆栈中的位置

public int search(Object o)

Queue

在这里插入图片描述
Queue的实现类:
在这里插入图片描述

queue接口中定义的一些方法,源码:

public interface Queue<E> extends Collection<E> {
    /**
     * Inserts the specified element into this queue if it is possible to do so
     * immediately without violating capacity restrictions, returning
     * {@code true} upon success and throwing an {@code IllegalStateException}
     * if no space is currently available.
     *
     * @param e the element to add
     * @return {@code true} (as specified by {@link Collection#add})
     * @throws IllegalStateException if the element cannot be added at this
     *         time due to capacity restrictions
     * @throws ClassCastException if the class of the specified element
     *         prevents it from being added to this queue
     * @throws NullPointerException if the specified element is null and
     *         this queue does not permit null elements
     * @throws IllegalArgumentException if some property of this element
     *         prevents it from being added to this queue
     */
    boolean add(E e);

    /**
     * Inserts the specified element into this queue if it is possible to do
     * so immediately without violating capacity restrictions.
     * When using a capacity-restricted queue, this method is generally
     * preferable to {@link #add}, which can fail to insert an element only
     * by throwing an exception.
     *
     * @param e the element to add
     * @return {@code true} if the element was added to this queue, else
     *         {@code false}
     * @throws ClassCastException if the class of the specified element
     *         prevents it from being added to this queue
     * @throws NullPointerException if the specified element is null and
     *         this queue does not permit null elements
     * @throws IllegalArgumentException if some property of this element
     *         prevents it from being added to this queue
     */
    boolean offer(E e);

    /**
     * Retrieves and removes the head of this queue.  This method differs
     * from {@link #poll poll} only in that it throws an exception if this
     * queue is empty.
     *
     * @return the head of this queue
     * @throws NoSuchElementException if this queue is empty
     */
    E remove();

    /**
     * Retrieves and removes the head of this queue,
     * or returns {@code null} if this queue is empty.
     *
     * @return the head of this queue, or {@code null} if this queue is empty
     */
    E poll();

    /**
     * Retrieves, but does not remove, the head of this queue.  This method
     * differs from {@link #peek peek} only in that it throws an exception
     * if this queue is empty.
     *
     * @return the head of this queue
     * @throws NoSuchElementException if this queue is empty
     */
    E element();

    /**
     * Retrieves, but does not remove, the head of this queue,
     * or returns {@code null} if this queue is empty.
     *
     * @return the head of this queue, or {@code null} if this queue is empty
     */
    E peek();
}

可以看出,queue也是实现了Collection,也有Collection拥有的方法。比如获得Iterator遍历等等…

介绍一下实现类:

(1)内置的不阻塞队列: PriorityQueue 和 ConcurrentLinkedQueue
  PriorityQueue 和 ConcurrentLinkedQueue 类在 Collection Framework 中加入两个具体集合实现。
  PriorityQueue 类实质上维护了一个有序列表。加入到 Queue 中的元素根据它们的天然排序(通过其 java.util.Comparable 实现)或者根据传递给构造函数的 java.util.Comparator 实现来定位。
  ConcurrentLinkedQueue 是基于链接节点的、线程安全的队列。并发访问不需要同步。因为它在队列的尾部添加元素并从头部删除它们,所以只要不需要知道队列的大 小,ConcurrentLinkedQueue 对公共集合的共享访问就可以工作得很好。收集关于队列大小的信息会很慢,需要遍历队列。

(2)实现阻塞接口的:
  五个阻塞队列类。它实质上就是一种带有一点扭曲的 FIFO 数据结构。不是立即从队列中添加或者删除元素,线程执行操作阻塞,直到有空间或者元素可用。
五个队列所提供的各有不同:
  ArrayBlockingQueue :一个由数组支持的有界队列。
  LinkedBlockingQueue :一个由链接节点支持的可选有界队列。
  PriorityBlockingQueue :一个由优先级堆支持的无界优先级队列。
  DelayQueue :一个由优先级堆支持的、基于时间的调度队列。
  SynchronousQueue :一个利用 BlockingQueue 接口的简单聚集(rendezvous)机制。

  remove   移除并返回队列头部的元素    如果队列为空,则抛出一个NoSuchElementException异常
  add        增加一个元索                     如果队列已满,则抛出一个IIIegaISlabEepeplian异常
  element  返回队列头部的元素             如果队列为空,则抛出一个NoSuchElementException异常
  offer       添加一个元素并返回true       如果队列已满,则返回false
  poll         移除并返问队列头部的元素    如果队列为空,则返回null
  peek       返回队列头部的元素             如果队列为空,则返回null
  put         添加一个元素                      如果队列满,则阻塞
  take        移除并返回队列头部的元素     如果队列为空,则阻塞

PriorityQueue

构造函数

public PriorityQueue()

public PriorityQueue(int initialCapacity)

public PriorityQueue(Collection<? extends E> c)

public PriorityQueue(PriorityQueue<? extends E> c)

public PriorityQueue(SortedSet<? extends E> c)

常用方法:

public boolean offer(E e)

public E peek()

public boolean remove(Object o)

public boolean contains(Object o)

public Object[] toArray()

public <T> T[] toArray(T[] a)

public Iterator<E> iterator()

public int size()

public void clear()

public E poll()

//返回用来对此队列中的元素进行排序的比较器;如果此队列根据其元素的 自然顺序进行排序,则返回 null。
public Comparator<? super E> comparator()

ArrayDeque

构造函数

public ArrayDeque()

public ArrayDeque(int numElements)

public ArrayDeque(Collection<? extends E> c)

常用方法

public boolean offerFirst(E e)

public boolean offerLast(E e)

public E pollFirst()

public E pollLast()

public E peekFirst()

public E peekLast()

public Iterator<E> iterator()

//逆向迭代器
public Iterator<E> descendingIterator()

//有无该元素
public boolean contains(Object o)

public int size()

//清除所有元素
public void clear()

public Object[] toArray()

public <T> T[] toArray(T[] a)

public ArrayDeque<E> clone()

这其中的add、remove方法没有添加,因为是空则抛出异常。因此,我们需要用空则返回boolean类型的函数。
此外,还有些从Queue接口继承的方法,也不列举了。

高级方法:

//移除此双端队列中第一次出现的指定元素(当从头部到尾部遍历双端队列时)。如果此双端队列不包含该元素,则不作更改。更确切地讲,移除第一个满足 o.equals(e) 的元素 e(如果存在这样的元素)。如果此双端队列包含指定的元素(或者此双端队列由于调用而发生了更改),则返回 true。
public boolean removeFirstOccurrence(Object o)

//移除此双端队列中最后一次出现的指定元素(当从头部到尾部遍历双端队列时)。如果此双端队列不包含该元素,则不作更改。更确切地讲,移除最后一个满足 o.equals(e) 的元素 e(如果存在这样的元素)。如果此双端队列包含指定的元素(或者此双端队列由于调用而发生了更改),则返回 true。
public boolean removeLastOccurrence(Object o)

此外,该队列还可以当做栈使用,来做栈的一些方法:

public E peek()

public void push(E e)

public E pop()

public boolean isEmpty()

public E element()
这篇关于Java学习笔记补充——Stack、Queue学习笔记的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!