Tutorial Playlist
191 Lessons1. Introduction to Java
2. What is Java?
3. History of Java
4. Java Tutorial for Beginners
5. How Do Java Programs Work?
6. JDK in Java
7. C++ Vs Java
8. Java vs. Python
9. Java vs. JavaScript
10. From Java Source Code to Executable
11. How to Install Java in Linux
12. How to Install Java in Windows 10
13. Java Hello World Program
14. Structure of Java Program and Java Syntax
15. Operators in Java
16. Java If-else
17. Switch Case In Java
18. Loops in Java
19. Infinite loop in Java
20. For Loop in Java
21. For Each Loop in Java
22. Constructor in Java
23. Constructor Overloading in Java
24. Copy Constructor in Java
25. Default Constructor in Java
26. Parameterized Constructors in Java
27. Constructor Chaining In Java
28. Finalize Method in Java
29. Static Method in Java
30. Equals Method in Java
31. Abstract Method in Java
32. toString() Method in Java
33. Difference between equals method in Java
34. Inheritance in Java
35. Multiple Inheritance in Java
36. Hierarchical Inheritance in Java
37. Java Classes and Objects
38. Scanner Class in java
39. All classes in java are inherited from which class
40. What is Nested Class in Java
41. POJO Class in Java
42. Anonymous Class in Java
43. Final Class in Java
44. Object Class in Java
45. Packages in Java
46. Access Modifiers in Java
47. Static Keyword In Java
48. Final Keyword in Java
49. Checked and Unchecked Exceptions in Java
50. User Defined Exception in Java
51. Error vs. Exception in Java
52. Java Collection
53. Collections in Java
54. Garbage Collection in Java
55. Generics In Java
56. Java Interfaces
57. Functional Interface in Java
58. Marker Interface in Java
59. Streams in Java
60. Byte stream in java
61. File Handling in Java
62. Thread in Java
63. Thread Lifecycle In Java
64. Daemon Thread in Java
65. Thread Priority in Java
66. Deadlock in Java
67. String Pool in Java
68. Java Database Connectivity(JDBC)
69. Design Patterns in Java
70. Functional Programming in Java
71. OOP vs Functional vs Procedural
72. Heap Memory and Stack Memory in Java
73. Applet in Java
74. Java Swing
75. Java Frameworks
76. Hibernate Framework
77. JUnit Testing
78. How to Install Eclipse IDE for Java?
79. Command line arguments in Java
80. Jar file in Java
81. Java Clean Code
82. OOPs Concepts in Java
83. Java OOPs Concepts
84. Overloading vs Overriding in Java
85. Java 8 features
86. String in Java
87. String to int in Java
88. Why String Is Immutable in Java?
89. Primitive Data Types in Java
90. Non-Primitive Data Types in Java
91. This and Super Keyword in Java
92. HashMap in Java
93. Comparable And Comparator in Java
94. Type Casting in Java
95. Arrays Sort in Java with Examples
96. Variable Hiding and Variable Shadowing in Java
97. Enum in Java
98. Substring in Java
99. Pattern Programs in Java
100. Hashcode in Java
101. What is ByteCode in Java?
102. How To Take Input From User in Java
103. GCD of Two Numbers in Java
104. Linked List in Java
105. Arithmetic Operators in Java
106. Conditional Operators in Java
107. Stack and Queue in Java
Now Reading
108. Array Length in Java
109. Number Pattern Program in Java
110. Split in java
111. Map In Java
112. Difference Between Throw and Throws in Java
113. Difference Between Data Hiding and Abstraction
114. HashSet in Java
115. String Length in Java
116. Factorial Using Recursion in Java
117. DateFormat in Java
118. StringBuilder Class in java
119. Instance variables in Java
120. Java List Size
121. Java APIs
122. Reverse an Array in Java
123. StringBuffer and StringBuilder Difference in Java
124. Java Program to Add Two Numbers
125. String to Array in Java
126. Regular Expressions in Java
127. Identifiers in Java
128. Data Structures in Java
129. Set in Java
130. Pass By Value and Call By Reference in Java
131. Try Catch in Java
132. Bubble Sort in Java
133. Caesar Cipher Program in Java
134. Queue in Java
135. Object Creation in Java
136. Multidimensional Array in Java
137. How to Read a File in Java
138. String Comparison in Java
139. Volatile Keyword in Java
140. Control Statements in Java
141. Jagged Array in Java
142. Two-Dimensional Array in Java
143. Java String Format
144. Replace in Java
145. charAt() in Java
146. CompareTo in Java
147. Matrix Multiplication in Java
148. Static Variable in Java
149. Event Handling in Java
150. parseInt in Java
151. Java ArrayList forEach
152. Abstraction in Java
153. String Input in Java
154. Logical Operators in Java
155. instanceof in Java
156. Math Floor in Java
157. Selection Sort Java
158. int to char in Java
159. Stringtokenizer in java
160. Implementing and Manipulating Abs in Java
161. Char array to string in java
162. Convert Double To String In Java
163. Deque in Java
164. Converting a List to an Array in Java
165. The Max function in java
166. Removing whitespace from string in java
167. String arrays in Java
168. Strings in Java Vs Strings in Cpp
169. Sum of digits of a number in Java
170. Art of Graphical User Interfaces
171. Trim in Java
172. RxJava
173. Recursion in Java
174. HashSet Java
175. Difference Between Java and Python
176. Square Root in Java
177. Reverse A String in Java
178. Even Odd Program in Java
179. Fibonacci Series in Java
180. Prime Number Program in Java
181. Java Program to Print Prime Numbers in a Given Range
182. Java Leap Year Program
183. Swapping of Two Numbers in Java
184. LCM of Two Numbers in Java
185. Math.sqrt() Function in Java
186. Area of Triangle in Java
187. Sort a String In Java
188. Factorial Program in Java
189. Javafx
190. Lambda expression in java
191. Setup Java Home and IDE on macOS
Stacks and queues are linear data structures that follow a particular order to add or remove entities.
In this tutorial, you will be introduced to stack and queue in Java. We will cover the critical concepts of stack and queue in Java, highlighting their practical application, implementations, functionalities, and how to develop stack and queue data structures using classes and objects.
Java uses stacks and queues as its fundamental data structures, but they can also be called classes. Both stacks and queues are linear data structures because elements are stored and accessed sequentially.
Understanding their principles and differences is crucial for effective programming and tackling coding interview questions. Let’s learn more about Java stacks and queues and look at Java stack and queue examples.
A stack in Java is a basic data structure that adheres to the Last-In-First-Out (LIFO) concept. It may be pictured as a stack of things, with the most recent item added being the initial one to be taken away. By allowing actions like push (including an element to the top) and pop (by eliminating and restoring the top element), stacks offer an effective approach to organizing and altering data
In Java, stacks are represented by the Stack class, a subclass of the Vector class.
Example:
import java.util.Stack;
public class StackExample {
public static void main(String[] args) {
// Create a stack
Stack<Integer> stack = new Stack<>();
// Push an element onto the stack
int element = 42;
stack.push(element);
// Pop the top element from the stack
int topElement = stack.pop();
System.out.println("Popped element: " + topElement);
// Peek at the top element of the stack
int peekElement = stack.peek();
System.out.println("Top element: " + peekElement);
// Check if the stack is empty
boolean isEmpty = stack.isEmpty();
System.out.println("Is stack empty? " + isEmpty);
// Get the size of the stack
int size = stack.size();
System.out.println("Size of the stack: " + size);
}
}
In this example, we first create a stack by declaring a variable of type Stack and use the new keyword to create an instance of the Stack class. You can replace DataType with the desired data type for the elements stored in the stack.
Then, we push an element onto the stack using the push() method to add an element to the top. We are using Integer as the data type for the stack. We set the element to 42 and push it onto the stack.
Finally, we use the pop() method to remove and retrieve the top element from the stack. The removed element is returned by this method, and the result is printed to the console.
Several methods are available to modify and access stack elements in the Java Stack class. Among the most common methods are:
1. push(element): Adds a new element to the stack's top.
2. pop(): Removes and returns the topmost element in the stack.
3. peek(): Returns the topmost stack element without deleting it.
4. empty(): Returns a boolean result that determines if the stack is empty.
5. size(): Gives the stack's element count.
Here is a real-world application (back-button functionality) of Java stacks:
In this example, the BrowserBackButton class represents the functionality of a browser's back button. The pageStack stack stores the URLs of visited pages. The visitPage() method adds a new page URL to the stack when a page is visited. The goBack() method pops the top URL from the stack and simulates navigating back to the previous page. If the stack is empty, it indicates no pages left to go back to.
In the main() method, we create an instance of BrowserBackButton and simulate visiting different web pages. Then, we call the goBack() method multiple times to simulate pressing the back button and navigating to previous pages. Finally, we try to go back when there are no more pages in the history, which shows the appropriate message.
import java.util.Stack;
public class BrowserBackButton {
private Stack<String> pageStack;
public BrowserBackButton() {
pageStack = new Stack<>();
}
public void visitPage(String url) {
// Add the URL to the stack when a new page is visited
pageStack.push(url);
System.out.println("Visited page: " + url);
}
public void goBack() {
if (!pageStack.isEmpty()) {
String previousPage = pageStack.pop();
System.out.println("Navigating back to: " + previousPage);
} else {
System.out.println("Cannot go back. No pages in history.");
}
}
public static void main(String[] args) {
BrowserBackButton browser = new BrowserBackButton();
// Visit different web pages
browser.visitPage("https://www.example.com");
browser.visitPage("https://www.example.com/page1");
browser.visitPage("https://www.example.com/page2");
// Press the back button to navigate back
browser.goBack();
browser.goBack();
browser.goBack();
browser.goBack(); // Trying to go back when no pages are left
}
}
A queue is a basic data structure representing a group of elements in a predetermined sequence. It adheres to the First-In-First-Out (FIFO) principle, which states that the first element added is also the first one taken out. And this is where the difference between stack and queue (in data structure terms) comes into the picture — while queue follows FIFO, stack follows LIFO data structure type.
In Java, we can use the Queue interface to represent a queue. Several classes implement the Queue interface in the Java Collections Framework, such as LinkedList and ArrayDeque.
Example:
import java.util.Queue;
import java.util.LinkedList;
public class QueueExample {
public static void main(String[] args) {
// Create a queue
Queue<Integer> queue = new LinkedList<>();
// Add an element to the queue
int element = 42;
queue.add(element);
// Remove the element from the front of the queue
int frontElement = queue.remove();
System.out.println("Removed element: " + frontElement);
// Peek at the element from the front of the queue
int peekElement = queue.peek();
System.out.println("Front element: " + peekElement);
// Check if the queue is empty
boolean isEmpty = queue.isEmpty();
System.out.println("Is queue empty? " + isEmpty);
// Get the size of the queue
int size = queue.size();
System.out.println("Size of the queue: " + size);
}
}
In this example, we first create a queue by declaring a variable of type Queue and use a class that implements the Queue interface. Then, we will use Integer as the data type for the queue. We set the element to the value 42 and add it to the queue using the add() method.
Finally, we will remove the element from the front of the queue using the remove() method to remove and retrieve the element from the front. The removed element is returned by this method, and we print the results to the console.
Java's Queue interface offers several ways to interact with queues. Here are a few of the common methods:
1. add(element): Adds a piece to the rear of the queue.
2. remove(): Removes and returns the element at the front of the queue.
3. isEmpty(): Checks if the queue is empty and returns a boolean value.
4. size(): Returns the number of elements in the queue.
1. Task Scheduling: Tasks are scheduled and managed systematically using queues.
2. Breadth-First Search (BFS): Implementing BFS algorithms for traversing graphs or trees requires queues.
3. Multi-threaded Synchronization: Thread-safe access is made possible through queues, which aid in resource sharing and synchronization across several threads.
Here is a real-world application (task scheduling) of Java queues:
In this example, the TaskScheduler class represents a task scheduling system. The taskQueue queue stores the incoming tasks. The enqueueTask() method adds a new task to the end of the queue. The processTasks() method continuously dequeues and processes tasks from the front of the queue until the queue becomes empty. Each task is simulated by a delay of 1 second.
In the main() method, we create an instance of TaskScheduler and enqueue multiple tasks. Then, we call the processTasks() method to start processing the tasks in the order they were enqueued.
import java.util.Queue;
import java.util.LinkedList;
public class TaskScheduler {
private Queue<String> taskQueue;
public TaskScheduler() {
taskQueue = new LinkedList<>();
}
public void enqueueTask(String task) {
// Enqueue a new task to the end of the queue
taskQueue.add(task);
System.out.println("Enqueued task: " + task);
}
public void processTasks() {
while (!taskQueue.isEmpty()) {
String task = taskQueue.remove();
System.out.println("Processing task: " + task);
// Simulate some processing time
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("All tasks processed.");
}
public static void main(String[] args) {
TaskScheduler scheduler = new TaskScheduler();
// Enqueue tasks
scheduler.enqueueTask("Task 1");
scheduler.enqueueTask("Task 2");
scheduler.enqueueTask("Task 3");
// Process tasks in the order they were enqueued
scheduler.processTasks();
}
}
In Java, queues can be implemented using the java.util.Queue interface or its subclasses. Here are some common types of queues:
1. LinkedList Queue: It provides efficient insertion and removal of components at both ends of the queue
2. ArrayDeque Queue: It provides dynamic array-based operations for adding and removing elements.
3. Priority Queue: It orders elements based on their natural ordering or a custom comparator, allowing for removal based on priority.
4. Blocking Queue: Provides additional blocking and synchronization operations for thread safety.
5. Synchronous Queue: It enables simultaneous element passing by acting as a meeting point between two threads.
Stack | Queue | |
1. | Stack is a data structure in Java. | Queue is also a data structure in Java. |
2. | It stores elements of the same type. | Similar elements are also stored there. |
3. | Both follow a specific order for adding and removing elements. | Both follow a specific order for adding and removing elements. |
4. | It allows adding (push/enqueue) and removing (pop/dequeue). | It also provides methods for adding elements (push/enqueue) and removing elements (pop/dequeue). |
5. | It can be used in various algorithms and applications. | It is also used in many algorithms and programs. |
Stack | Queue | |
1. | Follows the Last-In-First-Out (LIFO) principle. | Follows the First-In-First-Out (FIFO) principle. |
2. | Elements are added and removed from the top (end) of the stack. | Elements are added at the rear (end) and removed from the front (start) of the queue. |
3. | Examples of Stacks include function call stack and undo/redo functionality. | Examples in Queue include task scheduling and message passing. |
4. | Only the top element is accessible and removable at a time. | Both the front and rear elements are accessible and removable. |
5. | There is no easy way to access or eliminate middle-of-the-stack items. | When objects are in the middle of the line, it is difficult to access or remove them. |
Stacks and queues are essential Java data structures with specific functions. Counting function calls, doing undos and redos, and evaluating expressions are popular uses for stacks. Queues, on the other hand, are helpful for sequential data management, message passing, breadth-first search, and job scheduling.
By understanding its features, applications, and implementations, you can use stack and queue in Java to arrange and modify data methodically and efficiently.
1. Are stacks and queues thread-safe in Java?
There are thread-safe substitutes for Java's standard implementations of stacks and queues, such as java.util.Stack and java.util.LinkedList, which are not thread-safe by default.
2. Are there any limitations to the size of a stack or a queue in Java?
In Java, the size of a stack or queue is normally constrained by the amount of memory available on the machine, although certain particular implementations could have additional size restrictions.
3. How do I choose between using a stack or a queue in Java?
Java allows you to use either a stack or a queue depending on the needs of your application and whether you need to retain the order of insertion (queue) or prioritize the order of the items (stack).
PAVAN VADAPALLI
Director of Engineering
Director of Engineering @ upGrad. Motivated to leverage technology to solve problems. Seasoned leader for startups and fast moving orgs. Working …Read More
Popular
Talk to our experts. We’re available 24/7.
Indian Nationals
1800 210 2020
Foreign Nationals
+918045604032
upGrad does not grant credit; credits are granted, accepted or transferred at the sole discretion of the relevant educational institution offering the diploma or degree. We advise you to enquire further regarding the suitability of this program for your academic, professional requirements and job prospects before enrolling. upGrad does not make any representations regarding the recognition or equivalence of the credits or credentials awarded, unless otherwise expressly stated. Success depends on individual qualifications, experience, and efforts in seeking employment.
upGrad does not grant credit; credits are granted, accepted or transferred at the sole discretion of the relevant educational institution offering the diploma or degree. We advise you to enquire further regarding the suitability of this program for your academic, professional requirements and job prospects before enr...