What is the difference between the add and offer methods in a Queue in Java?

前端 未结 8 1495
盖世英雄少女心
盖世英雄少女心 2020-12-12 13:29

Take the PriorityQueue for example http://java.sun.com/j2se/1.5.0/docs/api/java/util/PriorityQueue.html#offer(E)

Can anyone give me an example of a Queu

8条回答
  •  不思量自难忘°
    2020-12-12 14:01

    There is no difference for the implementation of PriorityQueue.add:

    public boolean add(E e) {
        return offer(e);
    }
    

    For AbstractQueue there actually is a difference:

    public boolean add(E e) {
        if (offer(e))
            return true;
        else
            throw new IllegalStateException("Queue full");
    }
    

提交回复
热议问题