In this section, we will explore the concepts of Queue and Deque in Java. These data structures are part of the Java Collections Framework and are essential for managing ordered collections of elements.
- Introduction to Queue
A Queue is a collection designed for holding elements prior to processing. It follows the First-In-First-Out (FIFO) principle, where the first element added is the first one to be removed.
Key Characteristics of Queue:
- FIFO Order: Elements are processed in the order they were added.
- Operations: Common operations include
enqueue
(add an element) anddequeue
(remove an element).
Common Queue Implementations in Java:
LinkedList
PriorityQueue
ArrayDeque
Basic Queue Operations:
add(E e)
: Inserts the specified element into the queue.remove()
: Retrieves and removes the head of the queue.peek()
: Retrieves, but does not remove, the head of the queue.
Example Code:
import java.util.LinkedList; import java.util.Queue; public class QueueExample { public static void main(String[] args) { Queue<String> queue = new LinkedList<>(); // Adding elements to the queue queue.add("Element 1"); queue.add("Element 2"); queue.add("Element 3"); // Displaying the queue System.out.println("Queue: " + queue); // Removing the head of the queue String removedElement = queue.remove(); System.out.println("Removed Element: " + removedElement); // Peeking the head of the queue String head = queue.peek(); System.out.println("Head of Queue: " + head); // Displaying the queue after removal System.out.println("Queue after removal: " + queue); } }
Explanation:
- We create a
Queue
usingLinkedList
. - We add elements using the
add
method. - We remove the head of the queue using the
remove
method. - We peek at the head of the queue using the
peek
method.
- Introduction to Deque
A Deque (Double-Ended Queue) is a linear collection that supports element insertion and removal at both ends. It can function as both a queue and a stack.
Key Characteristics of Deque:
- Double-Ended: Elements can be added or removed from both ends.
- Operations: Supports all queue operations plus additional operations for both ends.
Common Deque Implementations in Java:
ArrayDeque
LinkedList
Basic Deque Operations:
addFirst(E e)
: Inserts the specified element at the front of the deque.addLast(E e)
: Inserts the specified element at the end of the deque.removeFirst()
: Retrieves and removes the first element of the deque.removeLast()
: Retrieves and removes the last element of the deque.peekFirst()
: Retrieves, but does not remove, the first element of the deque.peekLast()
: Retrieves, but does not remove, the last element of the deque.
Example Code:
import java.util.Deque; import java.util.LinkedList; public class DequeExample { public static void main(String[] args) { Deque<String> deque = new LinkedList<>(); // Adding elements to the deque deque.addFirst("Element 1"); deque.addLast("Element 2"); deque.addLast("Element 3"); // Displaying the deque System.out.println("Deque: " + deque); // Removing the first element String removedFirst = deque.removeFirst(); System.out.println("Removed First Element: " + removedFirst); // Removing the last element String removedLast = deque.removeLast(); System.out.println("Removed Last Element: " + removedLast); // Peeking the first element String first = deque.peekFirst(); System.out.println("First Element: " + first); // Peeking the last element String last = deque.peekLast(); System.out.println("Last Element: " + last); // Displaying the deque after removals System.out.println("Deque after removals: " + deque); } }
Explanation:
- We create a
Deque
usingLinkedList
. - We add elements to both ends using
addFirst
andaddLast
methods. - We remove elements from both ends using
removeFirst
andremoveLast
methods. - We peek at the first and last elements using
peekFirst
andpeekLast
methods.
- Practical Exercises
Exercise 1: Implement a Queue
Create a queue of integers and perform the following operations:
- Add the numbers 1, 2, 3, 4, and 5 to the queue.
- Remove the head of the queue.
- Peek at the head of the queue.
- Print the final state of the queue.
Solution:
import java.util.LinkedList; import java.util.Queue; public class QueueExercise { public static void main(String[] args) { Queue<Integer> queue = new LinkedList<>(); // Adding elements to the queue queue.add(1); queue.add(2); queue.add(3); queue.add(4); queue.add(5); // Removing the head of the queue queue.remove(); // Peeking at the head of the queue int head = queue.peek(); // Printing the final state of the queue System.out.println("Head of Queue: " + head); System.out.println("Final Queue: " + queue); } }
Exercise 2: Implement a Deque
Create a deque of strings and perform the following operations:
- Add "A" to the front and "B" to the end.
- Add "C" to the front and "D" to the end.
- Remove the first and last elements.
- Peek at the first and last elements.
- Print the final state of the deque.
Solution:
import java.util.Deque; import java.util.LinkedList; public class DequeExercise { public static void main(String[] args) { Deque<String> deque = new LinkedList<>(); // Adding elements to the deque deque.addFirst("A"); deque.addLast("B"); deque.addFirst("C"); deque.addLast("D"); // Removing the first and last elements deque.removeFirst(); deque.removeLast(); // Peeking at the first and last elements String first = deque.peekFirst(); String last = deque.peekLast(); // Printing the final state of the deque System.out.println("First Element: " + first); System.out.println("Last Element: " + last); System.out.println("Final Deque: " + deque); } }
- Summary
In this section, we covered the concepts of Queue and Deque in Java. We learned about their key characteristics, common implementations, and basic operations. We also provided practical examples and exercises to reinforce the concepts. Understanding these data structures is crucial for managing ordered collections of elements efficiently in Java.
Java Programming Course
Module 1: Introduction to Java
- Introduction to Java
- Setting Up the Development Environment
- Basic Syntax and Structure
- Variables and Data Types
- Operators
Module 2: Control Flow
Module 3: Object-Oriented Programming
- Introduction to OOP
- Classes and Objects
- Methods
- Constructors
- Inheritance
- Polymorphism
- Encapsulation
- Abstraction
Module 4: Advanced Object-Oriented Programming
Module 5: Data Structures and Collections
Module 6: Exception Handling
Module 7: File I/O
Module 8: Multithreading and Concurrency
- Introduction to Multithreading
- Creating Threads
- Thread Lifecycle
- Synchronization
- Concurrency Utilities
Module 9: Networking
- Introduction to Networking
- Sockets
- ServerSocket
- DatagramSocket and DatagramPacket
- URL and HttpURLConnection