Returns the first element in the deque without deleting it. Returns head of the deque (first element) or null if a queue is empty. Insert the given element e at the end of the deque without violating capacity restrictions. Insert the given element e to the front of the deque without violating capacity restrictions. Returns true on success and false on failure. Returns a standard iterator over the elements of the deque.Īdds given element e to the deque (as a tail) without violating capacity restrictions. Gets the last element of the deque without removing it. Retrieve the first element of the deque without removing it. Note that it does not delete the element. Returns the first element or head of the deque. This method returns reverse order iterator for the deque. Throws IllegalStateException if no space available in the deque.Īdds given element e to the front of the queue without violating capacity restrictions.Īdds element e to the last of the deque without violating capacity restrictions.Ĭhecks if the deque contains given element o. MethodĪdds given element e into the deque (at the tail) without violating capacity restrictions and returns true if success. Let’s summarize these methods in the below table. Besides, the deque interface provides the following methods that can be used to perform various operations with the deque object. import java.util.* Īs the deque interface implements a queue interface, it supports all the methods of the queue interface. We have just added some elements to the deque object and then printed them using a forEach loop. Here, we have used the ArrayDeque class to instantiate the deque interface. The following Java program demonstrates a simple example to better understand the deque. We can create a deque object using the ArrayDeque class as shown below: Deque deque_obj = new ArrayDeque () Deque Example The below diagram shows the hierarchy for the ArrayDeque class:Īs shown in the diagram, the ArrayDeque class inherits the AbstractCollection class and implements the Deque interface. Internally, the ArrayDeque class makes use of a dynamically resizable array that grows as the number of elements is increased. Deque is not thread-safe unless an external synchronization is provided.ĪrrayDeque belongs to java.util package.Deque does not support concurrent access by more than one thread.Array deques do not allow the use of Null values.Deque interface supports resizable arrays that can grow as required. ![]() Given below are a few important points to be noted about deque: Thus once the above deque objects are successfully created, they can use the functionality of the deque interface. ![]() Hence we can create deque objects using these two classes as shown below: Deque numdeque = new ArrayDeque () Deque strDeque = new LinkedList () The two classes below, implement the deque interface. import Īs the deque is an interface, we need concrete classes to implement the functionality of the deque interface. To use a deque interface in our program, we have to import the package that holds deque functionality using an import statement as shown below. As shown in the below diagram, the Deque interface extends to the Queue interface that in turn extends the Collection interface. ![]() The diagram below shows the hierarchy for the double-ended queue or deque. The features of undo or history mostly present in the web browsers can be implemented using deques. It can also be used to implement priority queues. A typical deque collection will look as shown below:ĭeque is mostly used to implement stack, queue, or list data structures.
0 Comments
Leave a Reply. |
Details
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |