WalzoneInterview Prep
πŸ“ž Interviewing soon? Practice with a realistic AI mock phone interview β€” it calls you, then scores you. First 15 min FREE β†’

Java Collections Β· Advanced Β· question 50 of 100

What is the difference between a List and a Queue in Java Collections, and when would you use one over the other?

πŸ“• Buy this interview preparation book: 100 Java Collections questions & answers β€” PDF + EPUB for $5

In Java Collections, a List is an ordered collection of elements that allows duplicate elements, while a Queue is also an ordered collection of elements that can contain duplicate elements, but it follows a first-in-first-out (FIFO) order. The main difference between the two is that a List supports random access and allows the addition or removal of elements at any position, whereas a Queue only allows the addition of elements at the end and removal of elements from the beginning.

Here is an example code that demonstrates the usage of a List and a Queue:

import java.util.*;

public class ListAndQueueExample {
    public static void main(String[] args) {
        // Example List
        List<String> list = new ArrayList<>();
        list.add("apple");
        list.add("banana");
        list.add("cherry");
        list.add("banana");
        System.out.println("List: " + list);

        // Example Queue
        Queue<String> queue = new LinkedList<>();
        queue.add("apple");
        queue.add("banana");
        queue.add("cherry");
        queue.add("banana");
        System.out.println("Queue: " + queue);

        // Accessing elements of the List
        System.out.println("The first element of the List is: " + list.get(0));

        // Accessing elements of the Queue
        System.out.println("The first element of the Queue is: " + queue.peek());

        // Removing elements from the List
        list.remove("banana");
        System.out.println("List after removing 'banana': " + list);

        // Removing elements from the Queue
        queue.remove();
        System.out.println("Queue after removing the first element: " + queue);
    }
}

Output:

List: [apple, banana, cherry, banana]
Queue: [apple, banana, cherry, banana]
The first element of the List is: apple
The first element of the Queue is: apple
List after removing 'banana': [apple, cherry, banana]
Queue after removing the first element: [banana, cherry, banana]

In general, you would use a List when you need to access elements at arbitrary positions, or when you need to add or remove elements at arbitrary positions. On the other hand, you would use a Queue when you need to maintain a first-in-first-out order, such as when implementing a task scheduler or a message queue.

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