![]() ![]() Removed element from the head of queue: 10 ("Head element of queue: " +peek) // Retrieves the head of queue without removing. ("Removed element at the head of queue: " +head) // Removes the head of queue. ("q.poll(): " +q.poll()) // Returns null because queue is empty. ("q.peak(): " +q.peek()) // Returns null because queue is empty. The removal operation takes place at the other end called head. Is new element added to the tail of queue: trueĮlements in queue after adding new element: Īs you can see in the above diagram, the adding operation takes place at one end of the queue called tail. ("Elements in queue after adding new element: " +q) Įlements in queue: Įlements in queue after removed: ![]() ("Is new element added to the tail of queue: " +addElement) ("Elements in queue after removed: " +q) ("Head element of queue: " +q.element()) Let’s take example programs to perform various operations based on the above methods defined by queue interface. This method is similar to the add() method inherited from the Collection interface, but the offer() method is more preferred for queues. The offer() method is used to insert an element to the queue. The peek() and element() methods are similar, except that peek() returns null object if the queue is empty, whereas element() throws anģ. The poll() and remove() methods are similar, except that poll() returns null object if the queue is empty, whereas remove() throws an exception named NoSuchElementException.Ģ. Methods inherited from Collection interface: addAll, remove, removeAll, isEmpty, clear, contains, containsAll, equals, hashCode, iterator, retainAll, size, toArray.ġ. It throws an exception named NoSuchElementException if the queue is empty. E remove(): It is used to retrieve and remove the element at head of the queue. This method returns null if this queue is empty.Ħ. E poll(): The poll() method is used to retrieve the element at the head of queue and removes the element in the process. E peek(): The peek() method is used to retrieve the element at the head of queue, but the element is not removed. This method returns true if e was added and false otherwise.Ĥ. boolean offer(E e): It is used to insert the specified element e to the queue. It throws an exception named NoSuchElementException if the queue is empty.ģ. E element(): It is used to retrieve the element at the head of queue, but the element is not removed from the head of the queue. IllegalStateException is thrown when we try to add an element to a fixed-length queue that is full.Ģ. IllegealArgumentException is thrown when an invalid argument is used. It throws an IllegalStateException if no space is currently available.Ī ClassCastException is thrown when an object is not compatible with elements in the queue.Ī NullPointerException is thrown when we attempt to store a null object because null elements are not allowed in the queue. boolean add(E e): This method is used to insert the specified element into the queue if the space is available without violating capacity restrictions. In addition to methods that Queue inherits from Collection, Queue defines several of its own methods that are as follows:ġ. Queue q = new ArrayDeque() Methods of Java Queue Interface When you need to create a queue, simply initialize a Queue variable with a LinkedList object as given below: Queue q = new LinkedList() ![]() For simple FIFO queues, LinkedList and ArrayDeque classes are the best choices to create a queue.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |