The course is part of this learning path
In this course, we'll learn the Collection framework and the Map Interface.
- Collection Framework vs Array
- Hierarchy of Collection Framework
- List interface - ArrayList Class
- Iterator Interface
- Set Interface
- Queue and Dequeue Interfaces
- Map Interface
- Anyone looking to get Oracle Java Certification
- Those who want to learn the Java Programming language from scratch
- Java developers who want to increase their knowledge
- Beginners with no previous coding experience in Java programming
- Those who want to learn tips and tricks in Oracle Certified Associate – Java SE 8 Programmer certification exams
- No prior knowledge is required about the Java programming language
- Basic computer knowledge
Hi there. In this video, we'll talk about the queue interfaces. The interface Queue is available in the java.util package and does extend the collection interface. It's used to keep the elements that are processed in the First In, First Out (FIFO) manner. Let's move on to the Eclipse and get some practice. First, I'll create a new class. So, I right-click on the collectionexample package and select the 'New', 'Class' options. The name of the class can be QueueExample, and I will check the checkbox for the main method and click the 'Finish' button. First, let's create an object from the Queue interface. But since the Queue is an interface, objects cannot be created of the type Queue. We always need a class that extends this list in order to create an object. For this, we will use the LinkedList class that implements the Deque interface. So, I write Queue<string> animalsQueue = new LinkedList<>( ).
Now, let's add some elements to this queue. animalsQueue.add("Dog"), animalsQueue.offer("Cat"). So, the offer and the add methods add the element to the queue. The difference is that if an error occurs during the addition process and the element cannot be added to the queue, the add method will throw an error and the application will crash. However, the offer method returns the null value preventing the application from crashing. I have used both to show the difference. But when working with the Queue, I will prefer the offer method. Let's continue now. animalsQueue.offer("Cow"), animalsQueue.offer("Lion"), animalsQueue.offer("Monkey"). Now, let's print the elements of this queue. System.out.println("animalsQueue : " + animalsQueue). And I'll print the size of the animalsQueue. System.out.println( "size of animalsQueue: " + animalsQueue.size( ) ). And let's print the first element of this queue. System.out.println( "The first element: " + animalsQueue.element( ) ). Also, we can use the peek method for retrieving the first element. I'll copy this line and paste it here and this method will be peek.
The element and peek methods also return the first element in the queue. The difference is, again, related to error throwing. If there is no element to return, the element method throws an error. But the peek method does not throw an error, it returns null. Now, let's remove an element from the queue and observe the removed element on the console. System.out.println( "Removed element: " + animalsQueue.poll( ) ). Also, you can use the remove method to delete an element from the queue. But if there is no element to remove, the remove method throws an exception and the app crashes, but the poll method returns the null value. Okay, lastly let's print the last version of the queue. I'll copy this line and paste it here. Now let's run and test the code. Elements have been successfully added to the queue with both add and offer methods. Currently, the size of the queue is 5. The first element in the queue is Dog. We have successfully printed the first element to the console using both the element and the peek methods. Then, we deleted an element from the queue with the poll method.
But if you notice, the deleted element is the first element in the queue, Dog. In other words, in the Queue interface, the elements are deleted from the queue in the order in which they were added to the queue. So, the FIFO (First In, First Out) rule applies. Remember that. And so this is the final version of the queue. If you want, let's use the remove method instead of the poll method and test it. I change it to remove. I will run it one more time. As you can see, the result has not changed. Again, the first element added to the queue is deleted from the queue. Yes, now let's comment on this line where we have done the deletion here and test the order in which the elements will be deleted from the queue with the while loop. while(IanimalsQueue.isEmpty( ) ). So, if there is an element in the queue, I'll delete the element with the poll method and print the deleted element on the console.
So, I write System.out.println( "Deleted element: " + animalsQueue.poll( ) ). So, this loop will continue until all the elements in the queue are deleted because our condition here is if not empty. If all elements in the queue are deleted, then the queue will be empty and the condition here will be false. In this case, the loop will terminate and in each loop, we'll observe the deleted element in the console. And lastly outside the loop, let's print the last version of the queue. Okay, let's run and see. As you can see, first, the Dog is deleted, then the Cat, then Cow, then Lion, and lastly Monkey. Notice the element which first added is first deleted from the queue. Remember the FIFO (First
In, First Out) and now the queue is empty. So, let's try to retrieve the first element and delete an element from the queue. I will copy the peek method and paste it here. Let's run it again. As you can see, the app didn't crash and the null value is returned. Now, let's use the element method instead. As you can see, this time, the NoSuchElementException is thrown. This is the difference between the element and peek methods. And now let's copy the remove method here and paste it here, and I'll convert this line to the command line. Let's run and see. As you can see, this time, the NoSuchElementException is thrown again. If you want, let's use the poll method. As you can see, the app didn't crash and the null value is returned. This is the difference between the remove and poll methods. Okay, the Queue interface is like that. Let's take a short break here. In the next lesson, we'll continue with the Deque interface. See you in the next lesson.
OAK Academy is made up of tech experts who have been in the sector for years and years and are deeply rooted in the tech world. They specialize in critical areas like cybersecurity, coding, IT, game development, app monetization, and mobile development.