C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Push: This adds an element to the front (or the top) of the ArrayDeque. The type must be matched that specified in the declaration.
Peek: Returns the frontmost (or topmost) element in the collection. It does not affect the ArrayDeque's contents.
Pop: This removes the topmost element of the ArrayDeque and returns it. Pop() throws an exception if empty.
Java program that uses ArrayDeque
import java.util.ArrayDeque;
public class Program {
public static void main(String[] args) {
// Create ArrayDeque with three elements.
ArrayDeque<Integer> deque = new ArrayDeque<>();
deque.push(10);
deque.push(500);
deque.push(1000);
// Peek to get the top item, but do not remove it.
int peekResult = deque.peek();
System.out.println(peekResult);
// Call pop on the Deque.
int popResult = deque.pop();
System.out.println(popResult);
// Pop again.
popResult = deque.pop();
System.out.println(popResult);
}
}
Output
1000
1000
500
For: The program loops over all the elements in the ArrayDeque with a for-each loop. No index is needed.
ForJava program that uses add, loops
import java.util.ArrayDeque;
public class Program {
public static void main(String[] args) {
ArrayDeque<String> deque = new ArrayDeque<>();
// Use add on ArrayDeque.
deque.add("caterpillar");
deque.add("dinosaur");
deque.add("bird");
// Loop over all the elements in the ArrayDeque.
for (String element : deque) {
System.out.println(element);
}
}
}
Output
caterpillar
dinosaur
bird
Size: This integer always equals 0 when the ArrayDeque is empty. The size() changes as elements are added and removed.
Clear: This removes all the elements in the ArrayDeque. To restart an algorithm, clear() is a simpler option than calling pop() many times.
Java program that uses isEmpty, size and clear
import java.util.ArrayDeque;
public class Program {
public static void main(String[] args) {
ArrayDeque<Double> doubles = new ArrayDeque<>();
// Add four elements to the ArrayDeque.
doubles.push(50.25);
doubles.push(100.40);
doubles.push(120.25);
doubles.push(150.50);
System.out.println(doubles.isEmpty());
System.out.println(doubles.size());
// Clear all elements from the ArrayDeque.
doubles.clear();
System.out.println(doubles.isEmpty());
System.out.println(doubles.size());
}
}
Output
false
4
true
0
Enum: We use an enum called Tag. We push our tags (None, Italics, Bold) to the ArrayDeque to help with parsing.
StringBuilder: We use a StringBuilder to build up a result from the parser. In the end this holds finished HTML.
StringBuilderLoop: In the loop, we see if the char is a star or underscore. And we manipulate the ArrayDeque based on its current contents.
Tip: This not complete code. But it shows how we use an ArrayDeque to manage a parser's state: we know whether to open or close a tag.
Java program that uses ArrayDeque for wiki parser
import java.util.ArrayDeque;
import java.lang.StringBuilder;
public class Program {
enum Tag {
None, Italics, Bold
}
public static void main(String[] args) {
String wiki = "How are *you my _friend_*?";
ArrayDeque<Tag> codes = new ArrayDeque<>();
StringBuilder builder = new StringBuilder();
// Loop over the string.
for (int i = 0; i < wiki.length(); i++) {
char value = wiki.charAt(i);
// Handle star or underscore characters.
if (value == '*') {
// Close italics if on stack, otherwise open it.
if (codes.peek() == Tag.Italics) {
codes.pop();
builder.append("</i>");
} else {
codes.push(Tag.Italics);
builder.append("<i>");
}
} else if (value == '_') {
// Close bold if on stack, otherwise open it.
if (codes.peek() == Tag.Bold) {
codes.pop();
builder.append("</b>");
} else {
codes.push(Tag.Bold);
builder.append("<b>");
}
} else {
// Append other characters.
builder.append(value);
}
}
// Display our results.
System.out.println(wiki);
System.out.println(builder);
}
}
Output
How are *you my _friend_*?
How are <i>you my <b>friend</b></i>?
Version 1: This version of the code creates and adds ten values to an ArrayDeque.
Version 2: Here we create a Stack, and push() elements to it. We perform the same number of operations as version 1.
Result: The ArrayDeque can be populated with 10 integers many times faster than a Stack. ArrayDeque should always be preferred.
Java program that times ArrayDeque and Stack
import java.util.ArrayDeque;
import java.util.Stack;
public class Program {
public static void main(String[] args) {
long t1 = System.currentTimeMillis();
// Version 1: push 10 elements to ArrayDeque.
for (int i = 0; i < 10000000; i++) {
ArrayDeque<Integer> deque = new ArrayDeque<>();
for (int v = 0; v < 10; v++) {
deque.push(v);
}
}
long t2 = System.currentTimeMillis();
// Version 2: push 10 elements to Stack.
for (int i = 0; i < 10000000; i++) {
Stack<Integer> stack = new Stack<>();
for (int v = 0; v < 10; v++) {
stack.push(v);
}
}
long t3 = System.currentTimeMillis();
// ... Print benchmark results.
System.out.println(t2 - t1);
System.out.println(t3 - t2);
}
}
Output
589 ms: ArrayDeque push
2664 ms: Stack push