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


相关推荐

  • 百度搜索引擎工作原理「建议收藏」

    百度搜索引擎工作原理「建议收藏」  关于百度以及其它搜索引擎的工作原理,其实大家已经讨论过很多,但随着科技的进步、互联网业的发展,各家搜索引擎都发生着巨大的变化,并且这些变化都是飞快的。我们设计这个章节的目的,除了从官方的角度发出一些声音、纠正一些之前的误读外,还希望通过不断更新内容,与百度搜索引擎发展保持同步,给各位站长带来最新的、与百度高相关的信息。本章主要内容分为四个章节,分别为:抓取建库;检索排序;外部投票;结果展现。…

    2022年7月18日
    16
  • 系统运维架构师体系[通俗易懂]

    系统运维架构师体系[通俗易懂]一、系统运维架构师体系1.系统运维架构体系排列:2.Linux运维架构的薪资水平:3.Linux运维的技能进化论4.Linux运维大致的知识框架4-1.Linux系统初级体系4-2.Linux系统中高级体系5.Linux运维的具体规划实践5-1.Linux运维基础5-2.Linux运维进阶6.Linux工作的必备要求7.Linux运维学习建议一、系统运维架构师体系1.系统运维架构体系排列:Linux运维工程师应用运维工程师,大数据运维工程师,运维开发工程师,云计算运维工程.

    2022年7月17日
    15
  • 安卓反编译_反编译apk工具

    安卓反编译_反编译apk工具刚刷了自己的小U(下次分享刷机经验),准备美化一下系统,这时需要对framework-res.apk进行编译和反编译,我也是边学习边实践,这里仅作分享。1、安装Java环境JDK↑Android是基于Linux的,而要在安卓上开发,基本上依靠Java为主。因为我们接下来要用到apktool,因此必须安装JDK。注意,JDK和Java环境不同,JDK是开发工具,你可以直接在Java官网下载,并能找…

    2022年9月18日
    0
  • getmethods和getdeclaredmethods_java中的method

    getmethods和getdeclaredmethods_java中的methodMethodgetDeclaredMethod(Stringname,Class…parameterTypes)d返回一个Method对象,该对象反映此Class对象所表示的类或接口的指定已声明方法。Method[]getDeclaredMethods()返回Method对象的一个数组,这些对象反映此Class对象表示的类或接口声明的所有方法,包括公共、保护、默认(包)访…

    2022年9月23日
    0
  • 会计初级学习笔记

    会计初级学习笔记

    2021年6月30日
    85
  • 全网最全Python学习路线图+14张思维导图,让python初学者不走弯路![通俗易懂]

    全网最全Python学习路线图+14张思维导图,让python初学者不走弯路![通俗易懂]        最近忙着做大数据的项目,故有一段时间没更新Python专栏的内容了。        突发奇想,想把当初自学python时收藏的关于python的14张思维导图和学习路线拿出来分享给许多同样处于”初级”阶段的各位攻城狮们。…

    2022年6月7日
    46

发表回复

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

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