Skip to content

Commit e13a2c9

Browse files
Create DequeDemo.java
1 parent ba98dd8 commit e13a2c9

File tree

1 file changed

+65
-0
lines changed

1 file changed

+65
-0
lines changed
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package learnCollections;
2+
3+
import java.util.ArrayDeque;
4+
import java.util.Deque;
5+
import java.util.LinkedList;
6+
7+
public class DequeDemo {
8+
public static void main(String[] args) {
9+
/*
10+
INSERTION METHODS
11+
12+
addFirst(E e) - adds element at the front of the deque, throws exception if deque is full
13+
offerFirst(E e) - adds element at the front of the deque, returns false if deque is full
14+
addLast(E e) - adds element at the end of the deque, throws exception if deque is full
15+
offerLast(E e) - adds element at the end of the deque, returns false if deque is full
16+
*/
17+
18+
/*
19+
REMOVAL METHODS
20+
21+
removeFirst() - removes and returns the first element of the deque, throws exception if deque is empty
22+
pollFirst() - removes and returns the first element of the deque, returns null if deque is empty
23+
removeLast() - removes and returns the last element of the deque, throws exception if deque is empty
24+
pollLast() - removes and returns the last element of the deque, returns null if deque is empty
25+
*/
26+
27+
/*
28+
EXAMINATION METHODS
29+
30+
getFirst() - retrieves, but does not remove, the first element of the deque, throws exception if deque is empty
31+
peekFirst() - retrieves, but does not remove, the first element of the deque, returns null if deque is empty
32+
getLast() - retrieves, but does not remove, the last element of the deque, throws exception if deque is empty
33+
peekLast() - retrieves, but does not remove, the last element of the deque, returns null if deque is empty
34+
*/
35+
36+
/*
37+
STACK METHODS (Deque can be used as a stack)
38+
39+
push(E e) - Adds an element at the front (equivalent to addFirst(E e)). Throws exception if deque is full
40+
pop() - Removes and returns the first element (equivalent to removeFirst()), throws exception if deque is empty
41+
*/
42+
43+
44+
Deque<Integer> deque1 = new ArrayDeque<>(); // faster iteration, low memory, no null allowed
45+
// circular array, head and tail pointers
46+
// no need to shift elements, just shift head and tail pointers
47+
deque1.addFirst(10);
48+
deque1.addLast(20);
49+
deque1.offerFirst(5);
50+
deque1.offerLast(25);
51+
System.out.println(deque1); // [5, 10, 20, 25]
52+
53+
System.out.println("First element: " + deque1.getFirst()); // 5
54+
System.out.println("Last element: " + deque1.getLast()); // 25
55+
deque1.removeFirst(); // removes 5
56+
deque1.pollLast(); // removes 25
57+
// Current Deque: [10, 20]
58+
59+
for(int num: deque1) {
60+
System.out.println(num); // 10, 20
61+
}
62+
63+
Deque<Integer> deque2 = new LinkedList<>(); // insertion, deletion somewhere in middle is faster, allows null elements
64+
}
65+
}

0 commit comments

Comments
 (0)