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

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

    from the source code in jdk 7 as follow:

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

    we can easily know that the add function will return true when successfully add a new element into the queue, but throw a exception when failed .

提交回复
热议问题