ThatGuy343
ThatGuy343

Reputation: 2424

Java ConcurrentLinkedQueue iteration element order?

Is the element currently being processed in a for loop, the head of the queue?

private Queue<User> users = new ConcurrentLinkedQueue<User>();

for(User u : users){
    users.remove(); // <- is this removing the currently iterated element?
}

Or is using users.remove(u) preferable here?

Upvotes: 1

Views: 3884

Answers (1)

John
John

Reputation: 5297

Yes, that is correct for ConcurrentLinkedQueue<E> since it orders elements in FIFO order.

From the docs:

This queue orders elements FIFO (first-in-first-out). The head of the queue is that element that has been on the queue the longest time. The tail of the queue is that element that has been on the queue the shortest time.

Upvotes: 3

Related Questions