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

前端 未结 8 1533
盖世英雄少女心
盖世英雄少女心 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 13:44

    The Queue interface specifies that add() will throw an IllegalStateException if no space is currently available (and otherwise return true) while offer() will return false if the element couldn't be inserted due to capacity restrictions.

    The reason they are the same in a PriorityQueue is that this queue is specified to be unbounded, i.e. there are no capacity restrictions. In the case of no capacity restrictions, the contracts of add() and offer() display the same behaviour.

提交回复
热议问题