0% found this document useful (0 votes)
7 views3 pages

Stack Vs Queue Methods in Java Landscape Explanations Last

The document compares Queue and Stack methods in Java, detailing their respective operations such as add, remove, peek, isEmpty, and size. It provides code examples for both Queue and Stack, demonstrating their functionalities. The Queue uses a LinkedList implementation while the Stack uses the Stack class in Java.

Uploaded by

relishmunjal07
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views3 pages

Stack Vs Queue Methods in Java Landscape Explanations Last

The document compares Queue and Stack methods in Java, detailing their respective operations such as add, remove, peek, isEmpty, and size. It provides code examples for both Queue and Stack, demonstrating their functionalities. The Queue uses a LinkedList implementation while the Stack uses the Stack class in Java.

Uploaded by

relishmunjal07
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Stack vs Queue Methods in Java

Queue Methods Stack Methods Explanation

add(E e) / offer(E e) add(E e) adds an


push(E
element
e) to the queue and offer(E e) returns false on failure. In Stack, push(E e) adds an ele

remove() / poll() remove() removes the front


pop()element of the queue. poll() does the same and returns null if empty. pop() removes the

peek() / element() peek() returns the front


peek()
element without removing it. element() is similar but throws an exception if empty. Stack p

isEmpty() isEmpty()
isEmpty() returns true if the queue is empty. In Stack, isEmpty() does the same.

size() size() size() returns the number of elements in the queue or stack.

Queue Example:

---------------

import java.util.Queue;

import java.util.LinkedList;

public class QueueExample {

public static void main(String[] args) {

Queue<String> queue = new LinkedList<>();


queue.add("Apple");

queue.add("Banana");

System.out.println("Queue size: " + queue.size()); // Output: 2

System.out.println("Head: " + queue.peek()); // Output: Apple

System.out.println("Removed: " + queue.remove()); // Output: Apple

System.out.println("Is queue empty? " + queue.isEmpty()); // Output: false

Stack Example:

--------------

import java.util.Stack;

public class StackExample {

public static void main(String[] args) {


Stack<String> stack = new Stack<>();

stack.push("Apple");

stack.push("Banana");

System.out.println("Stack size: " + stack.size()); // Output: 2

System.out.println("Top: " + stack.peek()); // Output: Banana

System.out.println("Popped: " + stack.pop()); // Output: Banana

System.out.println("Is stack empty? " + stack.isEmpty()); // Output: false

You might also like