views:

24

answers:

2

If I wanted to make a custom implementation of a Queue is correct to say that I can choose whatever order I want (not FIFO) but I must always respect the fact that the element to remove is that positioned as "head"?

but for insertion operation I'm not obliged to put the element to the tail (like into FIFO)?

+3  A: 

You are correct. The javadoc clearly states this:

Queues typically, but do not necessarily, order elements in a FIFO (first-in-first-out) manner. Among the exceptions are priority queues, which order elements according to a supplied comparator, or the elements' natural ordering, and LIFO queues (or stacks) which order the elements LIFO (last-in-first-out). Whatever the ordering used, the head of the queue is that element which would be removed by a call to remove() or poll(). In a FIFO queue, all new elements are inserted at the tail of the queue. Other kinds of queues may use different placement rules. Every Queue implementation must specify its ordering properties.

Justin
+1  A: 

You are correct.

The Queue documentation even states this explicitly (emphasis mine):

Queues typically, but do not necessarily, order elements in a FIFO (first-in-first-out) manner. [...] Whatever the ordering used, the head of the queue is that element which would be removed by a call to remove() or poll().

Joachim Sauer