java队列(Queue)用法总结[通俗易懂]

java队列(Queue)用法总结[通俗易懂]1.队列的特点队列是一种比较特殊的线性结构。它只允许在表的前端(front)进行删除操作,而在表的后端(rear)进行插入操作。进行插入操作的端称为队尾,进行删除操作的端称为队头。队列中最先插入的元素也将最先被删除,对应的最后插入的元素将最后被删除。因此队列又称为“先进先出”(FIFO—firstinfirstout)的线性表,与栈(FILO-firstinlastout)刚好相反…

大家好,又见面了,我是你们的朋友全栈君。

项目github地址:bitcarmanlee easy-algorithm-interview-and-practice
欢迎大家star,留言,一起学习进步

1.队列的特点

队列是一种比较特殊的线性结构。它只允许在表的前端(front)进行删除操作,而在表的后端(rear)进行插入操作。进行插入操作的端称为队尾,进行删除操作的端称为队头。
队列中最先插入的元素也将最先被删除,对应的最后插入的元素将最后被删除。因此队列又称为“先进先出”(FIFO—first in first out)的线性表,与栈(FILO-first in last out)刚好相反。

2.java中的队列

java中的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();
}

不得不说,JDK里的代码以及注释看着就是很舒服。各位稍微花点时间看看JDK里的源码以及注释,相信会有很多收获。

3.测试Queue接口

JDK中,LinkedList类实现了Queue接口,可以当Queue使用。

public class QueueTest {

    @Test
    public void test() {
        Queue<Integer> queue = new LinkedList<>();
        queue.offer(1);
        queue.offer(2);
        queue.offer(3);
        queue.offer(4);
        for(int e : queue) {
            System.out.println(e);
        }
        System.out.println("----------");
        System.out.println("poll : " + queue.poll());
        System.out.println("----------");

        for(int e : queue) {
            System.out.println(e);
        }
        System.out.println("ele is: " + queue.element());
        System.out.println("----------");

        for(int e : queue) {
            System.out.println(e);
        }

        System.out.println("peek : " + queue.peek());
        System.out.println("----------");

        for(int e : queue) {
            System.out.println(e);
        }
    }
}

最终代码运行的结果:

1
2
3
4
----------
poll : 1
----------
2
3
4
ele is: 2
----------
2
3
4
peek : 2
----------
2
3
4

结合第二部分内容,可以有以下结论:
1.尽量使用offer()方法添加元素,使用poll()方法移除元素。dd()和remove()方法在失败的时候会抛出异常。
2.peek方法不会删除元素, Retrieves, but does not remove, the head of this queue,

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/159004.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 有关二维码防封的问题的解决办法

    有关二维码防封的问题的解决办法

    2021年6月30日
    199
  • DDR2 ODT_ddr vtt电压

    DDR2 ODT_ddr vtt电压

    经常有人会说支持DDR2的主板存在偷工减料的现象。事实上这是由于DDR2内存中使用了一项新的ODT技术,它可以在提高内存信号稳定性的基础上节省不少电器元件(个人想法:ODT会增加功耗的阿)。主板终结是一种最为常见的终结主板内干扰信号的方法。在每一条信号传输路径的末端,都会安置一个终结电阻,它具备一定的阻值可以吸收反射回来的电子。但是目前DDR2内存的工作频率太高了,这种主板终结的方法并不能有效的阻止干扰信号。若硬要采用主板终结的方法得到纯净的DDR2时钟信号会花费巨额的制造成本。

    2022年9月9日
    0
  • linux解压rar包的命令[通俗易懂]

    linux解压rar包的命令[通俗易懂]upzipqq.war-d.\

    2022年7月11日
    16
  • docker(2)CentOS 7安装docker环境

    docker(2)CentOS 7安装docker环境前言前面一篇学了mac安装docker,这篇来学习在linux上安装docker环境准备Docker支持以下的CentOS版本,目前,CentOS仅发行版本中的内核支持Docker。Doc

    2022年8月6日
    3
  • bool类型_bool类型什么为真

    bool类型_bool类型什么为真转自:http://www.vcgood.com/archives/3709我们知道在C++里有专门的bool类型,用来表示真或假。但是在C语言里没有这样的类型(至少我是一直这么认为的),表达式的值0

    2022年8月5日
    3
  • apk逆向激活成功教程入门级[通俗易懂]

    apk逆向激活成功教程入门级[通俗易懂]样本很简单,就只有个发短信的行为,内容加密,可以直接写解密方法解密,但是这里我想通过hook解密方法直接动态看解密内容。动态跑发现并没有运行到解密方法那里,查看代码发现解密前有个if没通过:试着反编译修改代码找到对应的smali代码删除掉重新编译生成apk搞定。新生成的apk成功删除掉了if判断那块代码最后hook解密方法动态看到解密内容附上样本:链接:https://p…

    2022年9月2日
    2

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号