当前正在 for 循环中处理的元素是队列的头部吗?
private Queue<User> users = new ConcurrentLinkedQueue<User>();
for(User u : users){
users.remove(); // <- is this removing the currently iterated element?
}
或者这里使用 users.remove(u)
更好?
最佳答案
是的,ConcurrentLinkedQueue<E>
是正确的因为它按照 FIFO 顺序对元素进行排序。
来自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.
关于Java ConcurrentLinkedQueue 迭代元素顺序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30962928/