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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • matlab 二分法区间,多区间二分法[通俗易懂]

    &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp预备知识 二分法这里介绍一种多区间二分法,可以求出连续函数在某区间内几乎全部的根.方法就是把这个区间等分为若干个相等的小区间,然后分别判断这些小区间两端函数值的符号,对所有两端异号的区间使用二分法即可.显然,小区间的个数越多,越有可能找到所有的根.例程如下.代码1:bis…

    2022年4月11日
    126
  • pytest的assert_java单元测试断言

    pytest的assert_java单元测试断言前言断言是写自动化测试基本最重要的一步,一个用例没有断言,就失去了自动化测试的意义了。什么是断言呢?简单来讲就是实际结果和期望结果去对比,符合预期那就测试pass,不符合预期那就测试failed

    2022年7月30日
    6
  • SSL/TLS 双向认证(一) — SSL/TLS 工作原理

    SSL/TLS 双向认证(一) — SSL/TLS 工作原理本文部分参考:https://www.wosign.com/faq/faq2016-0309-03.htmhttps://www.wosign.com/faq/faq2016-0309-04.htmhttp://blog.csdn.net/hherima/article/details/52469674一:SSL/TLS介绍什么是SSL,什么是TLS呢?官话说SSL是安全套…

    2022年5月29日
    63
  • django常见问题_二级报错了怎么办

    django常见问题_二级报错了怎么办报错情况Django使用makemigrations做数据迁移的时候报如下错误File"/Users/jkc/.virtualenvs/django_env/lib/python3.7

    2022年7月31日
    11
  • java的api类_javaAPI类

    java的api类_javaAPI类扫描器Scanner可以用来从键盘输入读取数据extLine()获取一行字符串nextInt()获取一个整数nextFloat()获取一个浮点数大数字BigInteger如果我们需要操作一个很大的整数,它无法用long来表达时,就可以用BigIntegeradd()加subtract()减multiply()乘divide()除mod()取余包装类ØJDK提供了对所有数据类…

    2022年7月7日
    24
  • pycharm激活码2021.4【在线破解激活】

    pycharm激活码2021.4【在线破解激活】,https://javaforall.net/100143.html。详细ieda激活码不妨到全栈程序员必看教程网一起来了解一下吧!

    2022年3月16日
    64

发表回复

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

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