Deque (Doubly ended queue) is a data structure that allows you to insert and remove elements from both the ends.
The Deque interface was introduced in Java 6 in java.util.collection package.
Learn and master Java Collections Framework at Learn Java Collections Framework
The Deque interface extends the Queue interface and all methods provided by Queue are also available in the Deque interface.
Check out Java Queue Interface Example TutorialThe Deque interface defines methods to access the elements at both ends of the Deque instance. Methods are provided to insert, remove, and examine the elements.
There are three concrete implementations of the Deque interface: LinkedList, ArrayDeque, and LinkedBlockingDeque.
Deque interface methods
The below diagram shows a list of APIs/Methods that the Deque interface provides.
Deque interface Hierarchy Diagram
ArrayDeque implementation of Deque Interface Examples
Java ArrayDeque Example
import java.util.ArrayDeque;
import java.util.Deque;
public class ArrayDequeExample {
public static void main(String[] args) {
// Creating Deque and adding elements
Deque<String> deque = new ArrayDeque<String>();
deque.add("element1");
deque.add("element2");
deque.add("element3");
// Traversing elements
for (String str : deque) {
System.out.println(str);
}
}
}
Output:
element1
element2
element3
Java ArrayDeque Example: offerFirst() and pollLast()
package com.javaguides.collections.queueexamples;
import java.util.ArrayDeque;
import java.util.Deque;
public class DequeExample {
public static void main(String[] args) {
Deque<String> deque = new ArrayDeque<String>();
deque.offer("element1");
deque.offer("element2");
deque.add("element3");
deque.offerFirst("element4");
System.out.println("After offerFirst Traversal...");
for (String s : deque) {
System.out.println(s);
}
// deque.poll();
// deque.pollFirst();//it is same as poll()
deque.pollLast();
System.out.println("After pollLast() Traversal...");
for (String s : deque) {
System.out.println(s);
}
}
}
Output:
After offerFirst Traversal...
element4
element1
element2
element3
After pollLast() Traversal...
element4
element1
element2
package com.javaguides.collections.queueexamples;
import java.util.ArrayDeque;
import java.util.Deque;
public class DequeExample {
public static void main(String[] args) {
Deque<String> deque = new ArrayDeque<String>();
deque.offer("element1");
deque.offer("element2");
deque.add("element3");
deque.offerFirst("element4");
System.out.println("After offerFirst Traversal...");
for (String s : deque) {
System.out.println(s);
}
// deque.poll();
// deque.pollFirst();//it is same as poll()
deque.pollLast();
System.out.println("After pollLast() Traversal...");
for (String s : deque) {
System.out.println(s);
}
}
}
After offerFirst Traversal...
element4
element1
element2
element3
After pollLast() Traversal...
element4
element1
element2
Java ArrayDeque Example: Book Class
import java.util.ArrayDeque;
import java.util.Deque;
class Book {
int id;
String name, author, publisher;
int quantity;
public Book(int id, String name, String author, String publisher, int quantity) {
this.id = id;
this.name = name;
this.author = author;
this.publisher = publisher;
this.quantity = quantity;
}
}
public class ArrayDequeExample1 {
public static void main(String[] args) {
Deque<Book> set = new ArrayDeque<Book>();
// Creating Books
Book b1 = new Book(101, "Let us C", "Yashwant Kanetkar", "BPB", 8);
Book b2 = new Book(102, "Data Communications & Networking", "Forouzan", "Mc Graw Hill", 4);
Book b3 = new Book(103, "Operating System", "Galvin", "Wiley", 6);
// Adding Books to Deque
set.add(b1);
set.add(b2);
set.add(b3);
// Traversing ArrayDeque
for (Book b : set) {
System.out.println(b.id + " " + b.name + " " + b.author + " " + b.publisher + " " + b.quantity);
}
}
}
Output:
101 Let us C Yashwant Kanetkar BPB 8
102 Data Communications & Networking Forouzan Mc Graw Hill 4
103 Operating System Galvin Wiley 6
Related Collections Examples
- Java LinkedHashMap Example
- Java HashSet Example
- Java LinkedList Example
- Java ArrayList Example
- How To Remove Duplicate Elements From ArrayList In Java?
- Different Ways to Iterate over List, Set, and Map in Java
- Java Comparator Interface Example
- Java Comparable Interface Example
- Java IdentityHashMap Example
- Java WeakHashMap Example
- Java EnumMap Example
- Java CopyOnWriteArraySet Example
- Java EnumSet Class Example
- Guide to Java 8 forEach Method
- Different Ways to Iterate over a List in Java [Snippet]
- Different Ways to Iterate over a Set in Java [Snippet]
- Different Ways to Iterate over a Map in Java [Snippet]
- Iterate over TreeSet in Java Example
- Iterate over LinkedHashSet in Java Example
- Remove First and Last Elements of LinkedList in Java
- Iterate over LinkedList using an Iterator in Java
- Search an Element in an ArrayList in Java
- Iterate over ArrayList using Iterator in Java
- Remove Element from HashSet in Java
- Iterating over a HashSet using Iterator
Learn and master Java Collections Framework at Learn Java Collections Framework
Comments
Post a Comment
Leave Comment