问题描述
在使用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接口
希望如果有大牛路过的话可以在评论区评论分享技术,各自努力,你我最高处见