WalzoneInterview Prep
📞 Interviewing soon? Practice with a realistic AI mock phone interview — it calls you, then scores you. First 15 min FREE →

Java Collections · Intermediate · question 27 of 100

What is the difference between a Queue and a Stack in Java Collections?

📕 Buy this interview preparation book: 100 Java Collections questions & answers — PDF + EPUB for $5

Both Queue and Stack are data structures in Java Collections used to store a collection of elements. However, they have different ways of adding and removing elements, and retrieving the next element.

A Queue is a data structure that follows the FIFO (First In First Out) principle, meaning that the first element added to the queue is the first one to be removed. In Java, Queue is an interface that extends the Collection interface, and it has several implementing classes such as LinkedList, PriorityQueue, and ArrayDeque.

Here is an example of how to use a Queue in Java:

import java.util.LinkedList;
import java.util.Queue;

public class Example {
    public static void main(String[] args) {
        Queue<String> queue = new LinkedList<>();
        queue.add("first");
        queue.add("second");
        queue.add("third");
    
        while (!queue.isEmpty()) {
            System.out.println(queue.poll());
        }
    }
}

In this example, we create a Queue using the LinkedList class, and add three elements to it using the add() method. We then use a while loop to retrieve and print each element in the queue using the poll() method, which retrieves and removes the head of the queue.

A Stack, on the other hand, is a data structure that follows the LIFO (Last In First Out) principle, meaning that the last element added to the stack is the first one to be removed. In Java, Stack is a class that extends the Vector class, and it has three main methods: push(), pop(), and peek().

Here is an example of how to use a Stack in Java:

import java.util.Stack;

public class Example {
    public static void main(String[] args) {
        Stack<String> stack = new Stack<>();
        stack.push("first");
        stack.push("second");
        stack.push("third");
    
        while (!stack.isEmpty()) {
            System.out.println(stack.pop());
        }
    }
}

In this example, we create a Stack and add three elements to it using the push() method. We then use a while loop to retrieve and print each element in the stack using the pop() method, which retrieves and removes the top element of the stack.

In summary, the main difference between a Queue and a Stack in Java Collections is the way they add and remove elements, and retrieve the next element. A Queue follows the FIFO principle, while a Stack follows the LIFO principle.

Reading is step one. Saying it out loud is the interview. Our AI interviewer calls your phone and runs a realistic Java Collections interview — then scores it.
📞 Practice Java Collections — free 15 min
📕 Buy this interview preparation book: 100 Java Collections questions & answers — PDF + EPUB for $5

All 100 Java Collections questions · All topics