简体   繁体   中英

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM