Java Queue and Deque Interface

Last updated on Dec 27 2022
Prabhas Ramanathan

Java Queue interface orders the element in FIFO(First In First Out) manner. In FIFO, first element is removed first and last element is removed at last.

Table of Contents

Queue Interface declaration

1. public interface Queue<E> extends Collection<E>

Methods of Java Queue Interface

Method Description
boolean add(object) It is used to insert the specified element into this queue and return true upon success.
boolean offer(object) It is used to insert the specified element into this queue.
Object remove() It is used to retrieves and removes the head of this queue.
Object poll() It is used to retrieves and removes the head of this queue, or returns null if this queue is empty.
Object element() It is used to retrieves, but does not remove, the head of this queue.
Object peek() It is used to retrieves, but does not remove, the head of this queue, or returns null if this queue is empty.

PriorityQueue class

The PriorityQueue class provides the facility of using queue. But it does not orders the elements in FIFO manner. It inherits AbstractQueue class.

PriorityQueue class declaration

Let’s see the declaration for java.util.PriorityQueue class.
1. public class PriorityQueue<E> extends AbstractQueue<E> implements Serializable

Java PriorityQueue Example

1. import java.util.*; 
2. class TestCollection12{ 
3. public static void main(String args[]){ 
4. PriorityQueue<String> queue=new PriorityQueue<String>(); 
5. queue.add("Amit"); 
6. queue.add("Vijay"); 
7. queue.add("Karan"); 
8. queue.add("Jai"); 
9. queue.add("Rahul"); 
10. System.out.println("head:"+queue.element()); 
11. System.out.println("head:"+queue.peek()); 
12. System.out.println("iterating the queue elements:"); 
13. Iterator itr=queue.iterator(); 
14. while(itr.hasNext()){ 
15. System.out.println(itr.next()); 
16. } 
17. queue.remove(); 
18. queue.poll(); 
19. System.out.println("after removing two elements:"); 
20. Iterator<String> itr2=queue.iterator(); 
21. while(itr2.hasNext()){ 
22. System.out.println(itr2.next()); 
23. } 
24. } 
25. }

Test it Now
Output:head:Amit
head:Amit
iterating the queue elements:
Amit
Jai
Karan
Vijay
Rahul
after removing two elements:
Karan
Rahul
Vijay

Java PriorityQueue Example: Book

Let’s see a PriorityQueue example where we are adding books to queue and printing all the books. The elements in PriorityQueue must be of Comparable type. String and Wrapper classes are Comparable by default. To add user-defined objects in PriorityQueue, you need to implement Comparable interface.

1. import java.util.*; 
2. class Book implements Comparable<Book>{ 
3. int id; 
4. String name,author,publisher; 
5. int quantity; 
6. public Book(int id, String name, String author, String publisher, int quantity) { 
7. this.id = id; 
8. this.name = name; 
9. this.author = author; 
10. this.publisher = publisher; 
11. this.quantity = quantity; 
12. } 
13. public int compareTo(Book b) { 
14. if(id>b.id){ 
15. return 1; 
16. }else if(id<b.id){ 
17. return -1; 
18. }else{ 
19. return 0; 
20. } 
21. } 
22. } 
23. public class LinkedListExample { 
24. public static void main(String[] args) { 
25. Queue<Book> queue=new PriorityQueue<Book>(); 
26. //Creating Books 
27. Book b1=new Book(121,"Let us C","Yashwant Kanetkar","BPB",8); 
28. Book b2=new Book(233,"Operating System","Galvin","Wiley",6); 
29. Book b3=new Book(101,"Data Communications & Networking","Forouzan","Mc Graw Hill",4); 
30. //Adding Books to the queue 
31. queue.add(b1); 
32. queue.add(b2); 
33. queue.add(b3); 
34. System.out.println("Traversing the queue elements:"); 
35. //Traversing queue elements 
36. for(Book b:queue){ 
37. System.out.println(b.id+" "+b.name+" "+b.author+" "+b.publisher+" "+b.quantity); 
38. } 
39. queue.remove(); 
40. System.out.println("After removing one book record:"); 
41. for(Book b:queue){ 
42. System.out.println(b.id+" "+b.name+" "+b.author+" "+b.publisher+" "+b.quantity); 
43. } 
44. } 
45. }

Output:
Traversing the queue elements:
101 Data Communications & Networking Forouzan Mc Graw Hill 4
233 Operating System Galvin Wiley 6
121 Let us C Yashwant Kanetkar BPB 8
After removing one book record:
121 Let us C Yashwant Kanetkar BPB 8
233 Operating System Galvin Wiley 6

Java Deque Interface

Java Deque Interface is a linear collection that supports element insertion and removal at both ends. Deque is an acronym for “double ended queue”.

Deque Interface declaration

1. public interface Deque<E> extends Queue<E>

Methods of Java Deque Interface

Method Description
boolean add(object) It is used to insert the specified element into this deque and return true upon success.
boolean offer(object) It is used to insert the specified element into this deque.
Object remove() It is used to retrieves and removes the head of this deque.
Object poll() It is used to retrieves and removes the head of this deque, or returns null if this deque is empty.
Object element() It is used to retrieves, but does not remove, the head of this deque.
Object peek() It is used to retrieves, but does not remove, the head of this deque, or returns null if this deque is empty.

ArrayDeque class

The ArrayDeque class provides the facility of using deque and resizable-array. It inherits AbstractCollection class and implements the Deque interface.
The important points about ArrayDeque class are:
• Unlike Queue, we can add or remove elements from both sides.
• Null elements are not allowed in the ArrayDeque.
• ArrayDeque is not thread safe, in the absence of external synchronization.
• ArrayDeque has no capacity restrictions.
• ArrayDeque is faster than LinkedList and Stack.

ArrayDeque Hierarchy

The hierarchy of ArrayDeque class is given in the figure displayed at the right side of the page.

ArrayDeque class declaration

Let’s see the declaration for java.util.ArrayDeque class.
1. public class ArrayDeque<E> extends AbstractCollection<E> implements Deque<E>, Cloneable, Serializable

Java ArrayDeque Example

1. import java.util.*; 
2. public class ArrayDequeExample { 
3. public static void main(String[] args) { 
4. //Creating Deque and adding elements 
5. Deque<String> deque = new ArrayDeque<String>(); 
6. deque.add("Ravi"); 
7. deque.add("Vijay"); 
8. deque.add("Ajay"); 
9. //Traversing elements 
10. for (String str : deque) { 
11. System.out.println(str); 
12. } 
13. } 
14. }

Output:
Ravi
Vijay
Ajay

Java ArrayDeque Example: offerFirst() and pollLast()

1. import java.util.*; 
2. public class DequeExample { 
3. public static void main(String[] args) { 
4. Deque<String> deque=new ArrayDeque<String>(); 
5. deque.offer("arvind"); 
6. deque.offer("vimal"); 
7. deque.add("mukul"); 
8. deque.offerFirst("jai"); 
9. System.out.println("After offerFirst Traversal..."); 
10. for(String s:deque){ 
11. System.out.println(s); 
12. } 
13. //deque.poll(); 
14. //deque.pollFirst();//it is same as poll() 
15. deque.pollLast(); 
16. System.out.println("After pollLast() Traversal..."); 
17. for(String s:deque){ 
18. System.out.println(s); 
19. } 
20. } 
21. }

Output:
After offerFirst Traversal…
jai
arvind
vimal
mukul
After pollLast() Traversal…
jai
arvind
vimal

 

Java ArrayDeque Example: Book

1. import java.util.*; 
2. class Book { 
3. int id; 
4. String name,author,publisher; 
5. int quantity; 
6. public Book(int id, String name, String author, String publisher, int quantity) { 
7. this.id = id; 
8. this.name = name; 
9. this.author = author; 
10. this.publisher = publisher; 
11. this.quantity = quantity; 
12. } 
13. } 
14. public class ArrayDequeExample { 
15. public static void main(String[] args) { 
16. Deque<Book> set=new ArrayDeque<Book>(); 
17. //Creating Books 
18. Book b1=new Book(101,"Let us C","Yashwant Kanetkar","BPB",8); 
19. Book b2=new Book(102,"Data Communications & Networking","Forouzan","Mc Graw Hill",4); 
20. Book b3=new Book(103,"Operating System","Galvin","Wiley",6); 
21. //Adding Books to Deque 
22. set.add(b1); 
23. set.add(b2); 
24. set.add(b3); 
25. //Traversing ArrayDeque 
26. for(Book b:set){ 
27. System.out.println(b.id+" "+b.name+" "+b.author+" "+b.publisher+" "+b.quantity); 
28. } 
29. } 
30. }

Output:
101 Let us C Yashwant Kanetkar BPB 8
102 Data Communications & Networking Forouzan Mc Graw Hill 4
103 Operating System Galvin Wiley 6

 

So, this brings us to the end of blog. This Tecklearn ‘Java Queue and Deque Interface’ blog helps you with commonly asked questions if you are looking out for a job in Java Programming. If you wish to learn Java and build a career Java Programming domain, then check out our interactive, Java and JEE Training, that comes with 24*7 support to guide you throughout your learning period. Please find the link for course details:

Java and JEE Training

Java and JEE Training

About the Course

Java and JEE Certification Training is designed by professionals as per the industrial requirements and demands. This training encompasses comprehensive knowledge on basic and advanced concepts of core Java & J2EE along with popular frameworks like Hibernate, Spring & SOA. In this course, you will gain expertise in concepts like Java Array, Java OOPs, Java Function, Java Loops, Java Collections, Java Thread, Java Servlet, and Web Services using industry use-cases and this will help you to become a certified Java expert.

Why Should you take Java and JEE Training?

• Java developers are in great demand in the job market. With average pay going between $90,000/- to $120,000/- depending on your experience and the employers.
• Used by more than 10 Million developers worldwide to develop applications for 15 Billion devices.
• Java is one of the most popular programming languages in the software world. Rated #1 in TIOBE Popular programming languages index (15th Consecutive Year)

What you will Learn in this Course?

Introduction to Java

• Java Fundamentals
• Introduction to Java Basics
• Features of Java
• Various components of Java language
• Benefits of Java over other programming languages
• Key Benefits of Java

Installation and IDE’s for Java Programming Language

• Installation of Java
• Setting up of Eclipse IDE
• Components of Java Program
• Editors and IDEs used for Java Programming
• Writing a Simple Java Program

Data Handling and Functions

• Data types, Operations, Compilation process, Class files, Loops, Conditions
• Using Loop Constructs
• Arrays- Single Dimensional and Multi-Dimensional
• Functions
• Functions with Arguments

OOPS in Java: Concept of Object Orientation

• Object Oriented Programming in Java
• Implement classes and objects in Java
• Create Class Constructors
• Overload Constructors
• Inheritance
• Inherit Classes and create sub-classes
• Implement abstract classes and methods
• Use static keyword
• Implement Interfaces and use it

Polymorphism, Packages and String Handling

• Concept of Static and Run time Polymorphism
• Function Overloading
• String Handling –String Class
• Java Packages

Exception Handling and Multi-Threading

• Exception handling
• Various Types of Exception Handling
• Introduction to multi-threading in Java
• Extending the thread class
• Synchronizing the thread

File Handling in Java

• Input Output Streams
• Java.io Package
• File Handling in Java

Java Collections

• Wrapper Classes and Inner Classes: Integer, Character, Boolean, Float etc
• Applet Programs: How to write UI programs with Applet, Java.lang, Java.io, Java.util
• Collections: ArrayList, Vector, HashSet, TreeSet, HashMap, HashTable

Java Database Connectivity (JDBC)

• Introduction to SQL: Connect, Insert, Update, Delete, Select
• Introduction to JDBC and Architecture of JDBC
• Insert/Update/Delete/Select Operations using JDBC
• Batch Processing Transaction
• Management: Commit and Rollback

Java Enterprise Edition – Servlets

• Introduction to J2EE
• Client Server architecture
• URL, Port Number, Request, Response
• Need for servlets
• Servlet fundamentals
• Setting up a web project in Eclipse
• Configuring and running the web app with servlets
• GET and POST request in web application with demo
• Servlet lifecycle
• Servlets Continued
• Session tracking and filter
• Forward and include Servlet request dispatchers

Java Server Pages (JSP)

• Fundamentals of Java Server Page
• Writing a code using JSP
• The architecture of JSP
• JSP Continued
• JSP elements: Scriptlets, expressions, declaration
• JSP standard actions
• JSP directives
• Introduction to JavaBeans
• ServletConfig and ServletContext
• Servlet Chaining
• Cookies Management
• Session Management

Hibernate

• Introduction to Hibernate
• Introduction to ORM
• ORM features
• Hibernate as an ORM framework
• Hibernate features
• Setting up a project with Hibernate framework
• Basic APIs needed to do CRUD operations with Hibernate
• Hibernate Architecture

POJO (Plain Old Java Object)

• POJO (Plain Old Java Object)
• Persistent Objects
• Lifecycle of Persistent Object

Spring

• Introduction to Spring
• Spring Fundamentals
• Advanced Spring

Got a question for us? Please mention it in the comments section and we will get back to you.

 

 

0 responses on "Java Queue and Deque Interface"

Leave a Message

Your email address will not be published. Required fields are marked *