Queue questions
I was just perusing the Java Queue implementation.
Two things have me puzzled:
1. There is an offer method, and the description includes: "Inserts the specified element into this queue, if possible."
Under what conditions wouldn't the object be insertable? "Capacity bounds" are mentioned as a limiting reason, but I don't get this because when a Queue() is defined in Java, a queue length does not need to be mentioned. The only thing I can think is that "Capacity bounds" refers to available memory space?
2. Queue includes both poll() and remove(), which appear to be identical except that remove() throws an exception for an empty queue, while poll() returns null if the queue is empty.
A parallel comparison exists for element() and peek(); both retrieve the head of the queue without removing it, but element() throws an exception for a null queue while peek(), like poll(), returns null for an empty queue.
Why did the language designers of Java choose to have methods that are similar except for what they return in the case of an empty queue? I don't have sufficient experience to know if this is standard practice, or just in this particular case?
Thanks.
Two things have me puzzled:
1. There is an offer method, and the description includes: "Inserts the specified element into this queue, if possible."
Under what conditions wouldn't the object be insertable? "Capacity bounds" are mentioned as a limiting reason, but I don't get this because when a Queue() is defined in Java, a queue length does not need to be mentioned. The only thing I can think is that "Capacity bounds" refers to available memory space?
2. Queue includes both poll() and remove(), which appear to be identical except that remove() throws an exception for an empty queue, while poll() returns null if the queue is empty.
A parallel comparison exists for element() and peek(); both retrieve the head of the queue without removing it, but element() throws an exception for a null queue while peek(), like poll(), returns null for an empty queue.
Why did the language designers of Java choose to have methods that are similar except for what they return in the case of an empty queue? I don't have sufficient experience to know if this is standard practice, or just in this particular case?
Thanks.
