Queue队列中的add(E)和offer(E)方法的区别


问题描述

在使用PriorityQueue类的时候下面有offer和add方法都是用来在堆中插入元素,但是看一下add源码
    /**
     * Inserts the specified element into this priority queue.
     *
     * @return {@code true} (as specified by {@link Collection#add})
     * @throws ClassCastException if the specified element cannot be
     *         compared with elements currently in this priority queue
     *         according to the priority queue's ordering
     * @throws NullPointerException if the specified element is null
     */
    public boolean add(E e) {
        return offer(e);
    }
返回的还是offer方法,这不就是多此一举了吗,可是去搜索的结果的是告诉我这俩是有区别的
  
两者都是往队列尾部插入元素,不同的时候,当超出队列界限的时候,add()方法是抛出异常让你处理,而offer()方法是直接返回false

但是我不知道怎么看这俩个有这个区别,我只知道add方法继承的Collection方法,offer是实现的Queue接口

希望如果有大牛路过的话可以在评论区评论分享技术,各自努力,你我最高处见


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM