Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

FIFO based Queue implementations?

I need a simple FIFO implemented queue for storing a bunch of ints (I don't mind much if it is generics implementation).

Anything already baked for me in java.util or Trove/Guava library?

like image 600
Rajat Gupta Avatar asked Apr 18 '12 16:04

Rajat Gupta


People also ask

What is FIFO queue Java?

Answer: Queue in Java is a linear ordered data structure that follows FIFO (First In, First Out) ordering of elements. This means that the element inserted first in the queue will be the first element to be removed. In Java, the queue is implemented as an interface that inherits the Collection interface.

Which implementation of queue is best?

It's better to use ArrayDeque instead of LinkedList when implementing Stack and Queue in Java. ArrayDeque is likely to be faster than Stack interface (while Stack is thread-safe) when used as a stack, and faster than LinkedList when used as a queue.

What are the 3 primary methods for a queue?

The basic queue operations are: enqueue—adds an element to the end of the queue. dequeue—removes an element from the front of the queue. first—returns a reference to the element at the front of the queue.

What is a FIFO queue data structure?

A queue is a First-In First-Out (FIFO) data structure, commonly used in situations where you want to process items in the order they are created or queued. It is considered a limited access data structure since you are restricted to removing the oldest element first.


2 Answers

Yeah. Queue

LinkedList being the most trivial concrete implementation.

like image 166
John B Avatar answered Sep 23 '22 09:09

John B


Here is example code for usage of java's built-in FIFO queue:

public static void main(String[] args) {     Queue<Integer> myQ = new LinkedList<Integer>();     myQ.add(1);     myQ.add(6);     myQ.add(3);     System.out.println(myQ);   // 1 6 3     int first = myQ.poll();    // retrieve and remove the first element     System.out.println(first); // 1     System.out.println(myQ);   // 6 3 } 
like image 43
DavidNg Avatar answered Sep 21 '22 09:09

DavidNg