![]() ****************************************************************************** * Compilation: javac Queue.java * Execution: java Queue enqueue and dequeue * operations, along with methods for peeking at the first item, * testing if the queue is empty, and iterating through * the items in FIFO order. Using remove() and element() instead of poll() and peek(), respectively, would've had the same results, as the queue is never empty in our case.Below is the syntax highlighted version of Queue.java Here, our we'll gather the elements 3 and 4, but the first time the element will be removed (via poll()), and the second time not (via peek()), leaving our queue with element 4 in it. The remove() and element() methods will throw an exception when called on an empty Queue: Queue queue = new ArrayDeque() The two others, element() and peek() just return it but don't remove it. Two of them, remove() and poll(), remove the element before returning it. The interface offers a few methods to retrieve elements. Retrieving an ElementĪs stated earlier, a Queue generally respects FIFO, which means that it'll return the first entered element first, if we're retrieving one. On the other hand, using the offer() method instead will do nothing and return false as a result. Trying to add these two elements will result in: : Queue fullĪt java.base/(AbstractQueue.java:98) ![]() Therefore, we can't use the add() method two times consecutively without having an exception: Here, we've instantiated a queue which can hold a maximum of one element at a time. Instead of ArrayDeque, which is unbounded, let's use the LinkedBlockingQueue which can be assigned a capacity: Queue queue = new LinkedBlockingQueue( 1) On the other hand, the second one will return false in such cases. Well, the first will in fact never return false, rather throwing an Exception when adding an element to a full Queue. What's the difference between both methods then? Let's start with the former: queue.add( 3) īoth return a boolean value indicating if the element was added to the Queue or not, according to its capacity (if it applies). In order to add an element in this Queue, we have two possibilities: the add() method or the offer() method. First, let's instantiate one using the ArrayDeque implementation, which also implements the Deque interface we'll cover later: Queue queue = new ArrayDeque() We'll begin by adding an element to a Queue. Let's now explore the features of the Queue interface! Adding an Element The last one to be put on the top of the stack is the first one to be taken off. You can think of LIFO as a stack of coins. The first one to stand in the line is the first one to enter.īut there can be other implementations which respect the Last-In First-Out (LIFO) pattern, or even answer to some kind of priority system (e.g. You can think of FIFO as a line in front of a store. Generally, queues are returning elements respecting the First-In First-Out (FIFO) pattern, meaning the oldest element of the queue is returned first, then the oldest after that, etc. ![]() So, the idea is to push some elements into a Queue, and then retrieve them afterwards. Some can be of a fixed capacity, meaning they can contain only up to a certain number of elements. Principleįirst of all, what's it good for? The Queue is designed to hold elements prior to their processing. Let's start this final article of the series with the interface. This is the fourth and final part of a series of articles on Java Collections: Java Collections is a framework that provides numerous operations over a collection - searching, sorting, insertion, manipulation, deletion, etc. These classes mostly offer different ways to formulate a collection of objects within a single one. There are many collection classes in Java and all of them extend the and interfaces. The Java Collections Framework is a fundamental and essential framework that any strong Java developer should know like the back of their hand.Ī Collection in Java is defined as a group or collection of individual objects that act as a single object.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |