97

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?

1

5 Answers 5

103

Yeah. Queue

LinkedList being the most trivial concrete implementation.

Sign up to request clarification or add additional context in comments.

10 Comments

Notice that the Javadoc lists all the implementations. Also, the second link above it to LinkedList
LinkedList isn't an interface; it's an explicit class. Alternately, ArrayDeque is frequently faster.
I dont need to resize my queue anytime, the no of elemnts is always constant.
Are there any differences between ArrayDeque & ArrayQueue ?
This is not actually true: according to the javadoc Queue is not necessarily FIFO: docs.oracle.com/javase/7/docs/api/java/util/Queue.html. LinkedList can be used as a queue because the order is fixed though.
|
72

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
}

Comments

15

ArrayDeque is probably the fastest object-based queue in the JDK; Trove has the TIntQueue interface, but I don't know where its implementations live.

1 Comment

For ArrayDeque to function as a queue (FIFO) rather than a stack (LIFO), you should use add and remove. If you use push and pop, it behaves as a stack. (Strictly speaking, remove and pop are the same, but since add/pop or push/remove don't sound good as pairs, we go with add/remove and push/pop.)
12

A LinkedList can be used as a Queue - but you need to use it right. Here is an example code :

@Test
public void testQueue() {
    LinkedList<Integer> queue = new LinkedList<>();
    queue.add(1);
    queue.add(2);
    System.out.println(queue.pop());
    System.out.println(queue.pop());
}

Output :

1
2

Remember, if you use push instead of add ( which you will very likely do intuitively ), this will add element at the front of the list, making it behave like a stack.

So this is a Queue only if used in conjunction with add.

Try this :

@Test
public void testQueue() {
    LinkedList<Integer> queue = new LinkedList<>();
    queue.push(1);
    queue.push(2);
    System.out.println(queue.pop());
    System.out.println(queue.pop());
}

Output :

2
1

Comments

6

Queue is an interface that extends Collection in Java. It has all the functions needed to support FIFO architecture.

For concrete implementation you may use LinkedList. LinkedList implements Deque which in turn implements Queue. All of these are a part of java.util package.

For details about method with sample example you can refer FIFO based Queue implementation in Java.

PS: Above link goes to my personal blog that has additional details on this.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.