Contents
Collections
In this course, we'll learn the Collection framework and the Map Interface.
Learning Objectives
- Collection Framework vs Array
- Hierarchy of Collection Framework
- List interface - ArrayList Class
- Iterator Interface
- Set Interface
- Queue and Dequeue Interfaces
- Map Interface
Intended Audience
- 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
Prerequisites
- No prior knowledge is required about the Java programming language
- Basic computer knowledge
Hi there. In this video, we'll talk about the Deque Interfaces. The Deque interface present in the java.util package is a subtype of the Queue interface. The Deque is related to the double-ended queue that supports the addition or removal of elements from either end of the data structure. It can either be used as a queue, First In First Out (FIFO) or as a stack, Last In First Out( LIFO). Deque is the acronym for double-ended queue. 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 DequeExample and I will check the checkbox for the main method and click the 'Finish' button. Okay, first let's create an object from the Deque interface, but since the Deque is an interface, objects cannot be created out of the type queue. We always need a class that extends this list in order to create an object. For this, we'll use the LinkedList class that implements the Deque interface. So, I write Deque < string > <String> animalsDeque = new LinkedList<>( ).
The most important feature of the Deque interface is that we can add elements to both the beginning and the end of the queue. Now, we'll observe this step by step. Let's add an element to the animalsDeque with the classic add method, animalsDeque.add("Dog"); Let's add another element. animalsDeque.add("Cat"). Now, let's print this Deque on the console. System.out.println("animalsDeque: " + animalsDeque); Let's run and see. As you can see, it added both elements back to back as it should normally. In other words, the add method adds the element given as a parameter to the end of the list. Now, let's add an element to the beginning of the queue. animalsDeque.addFirst("Bird"). Let's run the application again. As you can see, the "Bird" element has been added to the beginning of the list. We can add an element to the beginning of the list with the addFirst method. Now let's add an element to the list using the addLast method. animalsDeque.addLast("Lion"). Let's run the application again. As you can see, the "Lion' element has been added to the end of the list.
We can add an element to the end of the list with the addLast method. Now, let's add one more element to the beginning of the list with another method. animalsDeque.push("Monkey"); Let's run the application again. As you can see, the "Monkey" element has been added to the beginning of the list. We can add an element to the beginning of the list with the push method. Now, let's add one more element to the end of the list with another method. animalsDeque.offer("Cow"); Let's run the application again. As you can see, the "Cow" element has been added to the end of the list. We can add an element to the end of the list with the offer method. Now let's add one more element to the beginning of the list with another method. animalsDeque.offerFirst("Horse"); Let's run the application again. As you can see, the "Horse" element has been added to the top of the list. We can add an element to the beginning of the list with the offerFirst method. Now, let's add one more element to the end of the list with another method. animalsDeque.offerLast("Sheep"); Let's run the application again.
As you can see, the "Sheep" element has been added to the end of the list. We can add an element to the end of the list with the offerLast method. Yes, as you can see it's possible to add elements to both the beginning and the end of the list. Now let's delete elements from both the beginning of the list and the end of the list. System.out.println("Delete element: " + animalsDeque.removeFirst()); Thus, we can see the deleted element on the console, and I will copy this line and paste it here. I want to see the last version of the list. Let's run and see. As you can see, the deleted element is horse. We can delete an element from the beginning of the list with the removeFirst method. And lastly, let's delete the last element. I'll copy these two lines and paste them here. This will be removeLast. Let's run and see. This time the sheep is deleted from the list. Okay, so we can add an element to the beginning and end of the list and also, we can delete an element from the beginning and end of the list. So, the Deque interface is like that. Let's take a short break here. See you in the next video.
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.