Java ConcurrentLinkedQueue 迭代元素顺序?

标签 java

当前正在 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/

相关文章:

java - 使用 Java/JDBC 时性能受到巨大影响

java - ProgressBar 不会在启动画面上逐渐显示进度

java - 在 Spring MVC 中返回 NoSuchRequestHandlingMethodException 404 错误的自定义 View

java - 事务部分提交或回滚

java - 将 JPanel 内容复制到另一个 JPanel,而不删除原始 JPanel 的内容

java - 在 Java 中验证 IPv4 字符串

javascript - 如何通过 linkText/linkName 严格地在表的隐藏行之一中找到特定链接(不使用 css-selectors 或 xpath)?

java - JPA级联删除: Setting child FK to NULL on a NOT NULL column

java - 是否可以在构造函数中使用此类实例?

java - 如何在Java中访问字符串中的特定索引?