Skip to content

Commit 143f0d6

Browse files
committed
feat: Queue VIP
Signed-off-by: https://github.com/Someshdiwan <[email protected]>
1 parent 430a56c commit 143f0d6

File tree

6 files changed

+45
-50
lines changed

6 files changed

+45
-50
lines changed
359 KB
Loading

Section 25 Collections Frameworks/Queue Interface/src/VIP/Key Features of Queue Interface.txt

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
Queue Interface in Java
2-
-----------------------
32

43
Definition:
4+
55
The Queue interface is part of the java.util package and is a child interface of the Collection interface.
6-
It represents a collection designed for **holding elements prior to processing**, typically following the
7-
**FIFO (First-In, First-Out)** principle.
6+
7+
It represents a collection designed for holding elements prior to processing, typically following the
8+
FIFO (First-In, First-Out) principle.
9+
810
However, some implementations (like PriorityQueue or Deque) provide different ordering policies.
911

1012
Key Features of Queue Interface:

Section 25 Collections Frameworks/Queue Interface/src/VIP/Queue Interface In Java.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
Queue Interface in Java
2-
-----------------------
32

43
Definition:
54
The Queue interface is part of the java.util package and is a child interface of the Collection interface.
5+
66
It represents a **First-In, First-Out (FIFO)** data structure, though some implementations may differ (like PriorityQueue).
77
A Queue models the concept of a **waiting line**, where elements are processed in the order they arrive.
88

Section 25 Collections Frameworks/Queue Interface/src/VIP/Queue — Theory, Internal Working.txt

Lines changed: 22 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
Queue — Theory, Internal Working:
22

33
Overview
4-
--------
4+
55
- `Queue` is an interface (java.util.Queue) modeling FIFO (first-in-first-out) behavior.
6-
- Multiple implementations exist: `LinkedList` (unbounded, non-blocking), `ArrayBlockingQueue` (bounded, blocking), `PriorityQueue` (not FIFO — ordered by comparator), `ConcurrentLinkedQueue`, etc.
7-
- Key difference: a `Queue` is a semantic contract (enqueue/dequeue), while `LinkedList` is a concrete data structure that can implement a queue.
6+
- Multiple implementations exist: `LinkedList` (unbounded, non-blocking), `ArrayBlockingQueue` (bounded, blocking),
7+
`PriorityQueue` (not FIFO — ordered by comparator), `ConcurrentLinkedQueue`, etc.
8+
9+
- Key difference: a `Queue` is a semantic contract (enqueue/dequeue), while `LinkedList` is a concrete
10+
data structure that can implement a queue.
811

912
Core queue methods and semantics
10-
-------------------------------
1113
- `add(e)`: inserts element; throws exception if capacity restricted and full.
1214
- `offer(e)`: inserts element; returns `false` if capacity restricted and full.
1315
- `remove()`: removes and returns head; throws exception if empty.
@@ -16,19 +18,17 @@ Core queue methods and semantics
1618
- `peek()`: returns head without removing; returns `null` if empty.
1719

1820
Queue internal variants and their internals
19-
------------------------------------------
20-
1. **LinkedList as Queue**
21+
1. LinkedList as Queue
2122
- Internally a doubly-linked list (see previous box).
2223
- `add`/`offer` at tail = O(1); `remove`/`poll` at head = O(1).
2324
- Unbounded (limited by memory).
2425

25-
ASCII:
26-
27-
Enqueue 1: [1]
28-
Enqueue 2: [1, 2]
29-
Dequeue(): 1 removed -> [2]
26+
ASCII:
27+
Enqueue 1: [1]
28+
Enqueue 2: [1, 2]
29+
Dequeue(): 1 removed -> [2]
3030

31-
2. **ArrayBlockingQueue**
31+
2. ArrayBlockingQueue
3232
- Fixed-size circular buffer (array) with head/tail indices.
3333
- For multi-thread usage, supports blocking operations (`put`, `take`) with locks/conditions.
3434
- `offer` vs `add` semantics differ only when full.
@@ -44,19 +44,18 @@ array: [10, _, _, _, _] head=0 tail=1
4444

4545
take(): val = array[head]; head=(head+1)%5; size–
4646

47-
3. **ConcurrentLinkedQueue**
47+
3. ConcurrentLinkedQueue
4848
- Lock-free, linked-node based queue using CAS (compare-and-swap).
4949
- High-concurrency non-blocking implementation suitable for many threads.
5050
- Iterators are weakly consistent (may or may not reflect concurrent updates).
5151

5252
Key differences between Queue implementations
53-
---------------------------------------------
54-
- **Threading & Blocking**
53+
- Threading & Blocking
5554
- `LinkedList`: not thread-safe (unless externally synchronized).
5655
- `ArrayBlockingQueue`: thread-safe, supports blocking operations (`put/take`).
5756
- `ConcurrentLinkedQueue`: thread-safe, non-blocking.
5857

59-
- **Capacity**
58+
- Capacity
6059
- `LinkedList`: unbounded.
6160
- `ArrayBlockingQueue`: bounded (fixed capacity).
6261
- `ConcurrentLinkedQueue`: effectively unbounded.
@@ -77,7 +76,6 @@ public class LinkedListAsQueue {
7776
System.out.println("Peek: " + q.peek()); // 2 (without removing)
7877
}
7978
}
80-
```
8179

8280
Example code: ArrayBlockingQueue (bounded, thread-safe)
8381

@@ -98,18 +96,17 @@ public class BoundedQueueExample {
9896
}
9997
}
10098
}
101-
```
10299

103100
When to use which queue
104-
• Use LinkedList/ArrayDeque for simple single-threaded FIFO needs.
105-
• Use ArrayBlockingQueue or LinkedBlockingQueue for producer-consumer bounded/unbounded blocking queues.
106-
• Use ConcurrentLinkedQueue for high-concurrency non-blocking queues.
107-
• Use PriorityQueue when you need priority ordering rather than FIFO.
101+
• Use LinkedList/ArrayDeque for simple single-threaded FIFO needs.
102+
• Use ArrayBlockingQueue or LinkedBlockingQueue for producer-consumer bounded/unbounded blocking queues.
103+
• Use ConcurrentLinkedQueue for high-concurrency non-blocking queues.
104+
• Use PriorityQueue when you need priority ordering rather than FIFO.
108105

109106
Performance summary
110-
• Enqueue/dequeue in LinkedList and ArrayDeque: O(1).
111-
• ArrayBlockingQueue: O(1) but may block threads.
112-
• ConcurrentLinkedQueue: expected O(1) amortized, highly concurrent.
107+
• Enqueue/dequeue in LinkedList and ArrayDeque: O(1).
108+
• ArrayBlockingQueue: O(1) but may block threads.
109+
• ConcurrentLinkedQueue: expected O(1) amortized, highly concurrent.
113110

114111
ASCII example overview
115112

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,17 @@
1-
Quick Differences: LinkedList vs Queue (Concept)
2-
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
1+
Quick Differences: LinkedList vs Queue
32

3+
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
44
| Aspect | LinkedList (Concrete Class) | Queue (Interface / Concept) |
55
|---------------------|----------------------------------------------------|-----------------------------------------------------------------------------------------------------------------------------------|
6-
| **Type** | Concrete data structure (implements List, Deque) | Abstract contract that defines FIFO behavior |
7-
| **Hierarchy** | Extends `AbstractSequentialList`, implements `List`, `Deque`, `Queue` | Belongs to `java.util`, implemented by `LinkedList`, `PriorityQueue`, `ArrayDeque`, `ArrayBlockingQueue`, etc. |
8-
| **Primary Purpose** | General-purpose doubly-linked list (list + deque) | First-In-First-Out (FIFO) semantics (enqueue/dequeue) |
9-
| **Data Structure** | Doubly-linked nodes (each node has `prev`, `next`) | Depends on implementation: linked list, array, skip list, heap |
10-
| **Ordering** | Maintains insertion order | FIFO ordering (or priority-based for `PriorityQueue`) |
11-
| **Random Access** | Supported via index (`get(int)`), but O(n) | Not always supported; depends on implementation |
12-
| **Insert/Remove** | Fast at head/tail (`addFirst`, `removeLast`) | `offer()` adds to tail, `poll()`/`remove()` removes from head |
13-
| **Null Handling** | Allows multiple nulls | Depends: `LinkedList` allows nulls, but `PriorityQueue` / concurrent queues may disallow |
14-
| **Thread-Safety** | Not thread-safe (must synchronize externally) | Varies: `ArrayBlockingQueue`, `ConcurrentLinkedQueue` are thread-safe |
15-
| **Iteration** | Bidirectional (`ListIterator`) | Depends on implementation; usually forward-only FIFO |
16-
| **Use Cases** | When you need both **list-like** (random access) and **deque-like** behavior | When you need pure queue semantics (task scheduling, producer-consumer, buffering) |
17-
18-
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
6+
| Type | Concrete data structure (implements List, Deque) | Abstract contract that defines FIFO behavior |
7+
| Hierarchy | Extends `AbstractSequentialList`, implements `List`, `Deque`, `Queue` | Belongs to `java.util`, implemented by `LinkedList`, `PriorityQueue`, `ArrayDeque`, `ArrayBlockingQueue`, etc. |
8+
| Primary Purpose | General-purpose doubly-linked list (list + deque) | First-In-First-Out (FIFO) semantics (enqueue/dequeue) |
9+
| Data Structure | Doubly-linked nodes (each node has `prev`, `next`) | Depends on implementation: linked list, array, skip list, heap |
10+
| Ordering | Maintains insertion order | FIFO ordering (or priority-based for `PriorityQueue`) |
11+
| Random Access | Supported via index (`get(int)`), but O(n) | Not always supported; depends on implementation |
12+
| Insert/Remove | Fast at head/tail (`addFirst`, `removeLast`) | `offer()` adds to tail, `poll()`/`remove()` removes from head |
13+
| Null Handling | Allows multiple nulls | Depends: `LinkedList` allows nulls, but `PriorityQueue` / concurrent queues may disallow |
14+
| Thread-Safety | Not thread-safe (must synchronize externally) | Varies: `ArrayBlockingQueue`, `ConcurrentLinkedQueue` are thread-safe |
15+
| Iteration | Bidirectional (`ListIterator`) | Depends on implementation; usually forward-only FIFO |
16+
| Use Cases | When you need both **list-like** (random access) and **deque-like** behavior | When you need pure queue semantics (task scheduling, producer-consumer, buffering) |
17+
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

Section 25 Collections Frameworks/Queue Interface/src/VIP/Working Queue Interface In Java.txt

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ Key Features of Queue:
3131
- `ConcurrentLinkedQueue`, `ArrayBlockingQueue`, `PriorityBlockingQueue` provide thread-safe operations.
3232

3333
Queue Interface Hierarchy (Simplified)
34-
--------------------------------------
34+
3535
Queue (interface)
3636
3737
├── Deque (interface) → Double-ended queue, supports insertion/removal from both ends
@@ -47,8 +47,8 @@ Queue (interface)
4747
├── PriorityBlockingQueue → Priority-based ordering with blocking
4848
└── DelayQueue / SynchronousQueue etc. for advanced use-cases
4949

50+
5051
ASCII Conceptual View (FIFO Queue)
51-
----------------------------------
5252

5353
Offer/Enqueue → [10] → [20] → [30] → [40] → [50] → ...
5454
↑head ↑tail
@@ -58,27 +58,24 @@ Poll/Dequeue removes from head:
5858
new head = 20
5959

6060
Special Case (PriorityQueue)
61-
----------------------------
61+
6262
Internally uses a binary heap:
6363
[5, 10, 15, 20]
6464
Root (5) is always the minimum (or maximum, depending on comparator).
6565
poll() removes the root, heap is restructured.
6666

6767
When to Use Queue
68-
-----------------
6968
- When you need FIFO ordering (task scheduling, buffer management).
7069
- When you want priority-based processing (PriorityQueue).
7170
- In concurrent applications (producer-consumer) → use BlockingQueue variants.
7271

7372
Practical Examples
74-
------------------
7573
1. Printer spooler: tasks queued in order of arrival.
7674
2. OS job scheduling: processes waiting in FIFO or priority order.
7775
3. Producer-consumer design: producers put items in a queue, consumers take items from it.
7876
4. Real-time systems: DelayQueue, SynchronousQueue, etc. for advanced coordination.
7977

8078
Complexity
81-
----------
8279
- offer/add: O(1) in linked or array-backed queues.
8380
- poll/remove: O(1) in FIFO queues; O(log n) in PriorityQueue (heap-based).
8481
- peek/element: O(1).

0 commit comments

Comments
 (0)