Sobes.tech
Junior

Which collection in Java implements the FIFO (first-in, first-out) service discipline?

sobes.tech AI

Answer from AI

Interface Queue.

Its implementations:

  • LinkedList
  • PriorityQueue
  • ArrayDeque
  • ConcurrentLinkedQueue

The most commonly used is LinkedList, as it also implements the List interface. ArrayDeque is preferred for use as a stack or queue where a fixed size is required. PriorityQueue implements a priority queue, and ConcurrentLinkedQueue is a thread-safe queue.

Example of using LinkedList as a queue:

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

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

queue.offer("element 1"); // Add element to the end of the queue
queue.offer("element 2");
queue.offer("element 3");

System.out.println(queue.poll()); // Remove and return the first element ("element 1")
System.out.println(queue.peek()); // Return the first element without removing ("element 2")
Which collection in Java implements the FIFO… - sobes.tech